访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?

access to boost:shared_ptr outside main scope crashes with Assertion failed: px != 0. What is the correct use of a pointer?

本文关键字:指针 px 是什么 用法 失败 断言 shared ptr 崩溃 范围 访问      更新时间:2023-10-16

访问类型为 boost:shared_ptr 的云崩溃并出现断言失败:px != 主外部错误为 0,但在 main 内部没问题。

我将在Qt程序中使用PCL,我需要访问指向MainWindow::classxyz((中声明f.ex的范围之外的云指针,所以我编写了这个测试程序来说明我的问题(见下文(

如何正确使用指针才能访问 main 范围之外的云指针?(和Qt,在MainWindow:MainWindow((的范围之外,因为我将在构造函数中初始化指针(

pcd_read.h:

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
void outside();

pcd_read.cpp:

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "pcd_read.h"
int
main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("C:/Users/user2/Documents/qt_test_kode_div/pcd_file_scope_test/build/Debug/test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
std::cout << cloud->size();     //This works
outside();                                //When I call outside() the code crashes inside outside()
for (size_t i = 0; i < cloud->points.size (); ++i)
std::cout << "    " << cloud->points[i].x
<< " "    << cloud->points[i].y
<< " "    << cloud->points[i].z << std::endl;

return (0);
}
void outside()
{
std::cout << cloud->size();         // This crashes. Why does accessing cloud cause a crash related to Boost? Assertion failed: px != 0
// The pointer seems to not be initialized. 
// I want the pointer to be accessible also in outside without passing as a parameter. How can I achieve that?
}

您在main中声明了另一个同名cloud变量。

因此,全局的在main中是不可见的,并且一直未使用,直到您调用outside,然后它仍然引用未使用的全局。

正如@chrisD提到的,将指针初始化更改为常规指针解决了这个问题。

.h:

//pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;    // original -- CRASHES OUTSIDE SCOPE - smartpointer ... 
pcl::PointCloud<pcl::PointXYZ> *cloud;          // changed to normal pointer -- now no crash ... since it is inside scope
void outside();

。.cpp

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "pcd_read.h"
int
main (int argc, char** argv)
{
//pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);        // Causes a crash when cloud is outside of scope
cloud = new pcl::PointCloud<pcl::PointXYZ>();    //Initializes pointer the std. way instead
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("C:/Users/user2/Documents/qt_test_kode_div/pcd_file_scope_test/build/Debug/test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
std::cout << cloud->size();     //This works
outside();
for (size_t i = 0; i < cloud->points.size (); ++i)
std::cout << "    " << cloud->points[i].x
<< " "    << cloud->points[i].y
<< " "    << cloud->points[i].z << std::endl;

return (0);
}
void outside()
{
std::cout << cloud->size();         // Now OK