C++ 访问boost:shared_ptr超出主作用域崩溃,断言失败:px!=0指针的正确用法是什么?
访问类型为boost:shared_ptr的云崩溃,断言失败:px!=main外部0错误,但main内部正常 我将在一个Qt程序中使用PCL,在这个程序中,我需要访问指向范围外的云的指针,该指针在MainWindow::classxyz()中声明为f.ex,因此我编写了这个测试程序来说明我的问题(见下文) 如何正确使用指针以访问main范围之外的云指针?(和Qt,在MainWindow:MainWindow()的范围之外,因为我将在构造函数中初始化指针) pcd_read.h:C++ 访问boost:shared_ptr超出主作用域崩溃,断言失败:px!=0指针的正确用法是什么?,c++,boost,point-cloud-library,C++,Boost,Point Cloud Library,访问类型为boost:shared_ptr的云崩溃,断言失败:px!=main外部0错误,但main内部正常 我将在一个Qt程序中使用PCL,在这个程序中,我需要访问指向范围外的云的指针,该指针在MainWindow::classxyz()中声明为f.ex,因此我编写了这个测试程序来说明我的问题(见下文) 如何正确使用指针以访问main范围之外的云指针?(和Qt,在MainWindow:MainWindow()的范围之外,因为我将在构造函数中初始化指针) pcd_read.h: pcl::Poi
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
void outside();
pcl::PointCloud::Ptr cloud;
外空();
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?
}
#包括
#包括
#包括
#包括“pcd_read.h”
int
主(内部argc,字符**argv)
{
pcl::PointCloud::Ptr cloud(新的pcl::PointCloud);
如果(pcl::io::loadPCDFile(“C:/Users/user2/Documents/qt\u test\u kode\u div/pcd\u file\u scope\u test/build/Debug/test\u pcd.pcd”,*cloud)==-1)/*加载文件
{
PCL_错误(“无法读取文件test_pcd.pcd\n”);
返回(-1);
}
标准高度
您在main
中声明了另一个同名的变量cloud
因此,全局指针在main
内部不可见,并且未被使用,直到您调用outside
,它仍然引用未使用的全局指针。如@chrisD所述,将指针初始化更改为常规指针解决了此问题
.h:
//pcl::PointCloud::Ptr cloud;//原始--超出范围崩溃-smartpointer。。。
pcl::PointCloud*cloud;//更改为普通指针--现在没有崩溃…因为它在范围内
外空();
.cpp
#包括
#包括
#包括
#包括“pcd_read.h”
int
主(内部argc,字符**argv)
{
//pcl::PointCloud::Ptr cloud(新pcl::PointCloud);//在云超出范围时导致崩溃
cloud=new pcl::PointCloud();//以标准方式初始化指针
如果(pcl::io::loadPCDFile(“C:/Users/user2/Documents/qt\u test\u kode\u div/pcd\u file\u scope\u test/build/Debug/test\u pcd.pcd”,*cloud)==-1)/*加载文件
{
PCL_错误(“无法读取文件test_pcd.pcd\n”);
返回(-1);
}
标准高度
外部使用的cloud
与main
设置的cloud
变量不同。一个是全局变量,另一个是main
中的局部变量。如果在类中将其声明为private,我会遇到同样的问题。我可以在初始化它的构造函数中访问它。但是在另一个同一个类的r方法,我得到了一个崩溃。所以这不仅仅是错误的作用域。因为编译器应该抱怨我所期望的?所以当你这样做时,你会遇到类似或不同的问题。遗憾的是,我无法帮助处理我看不到的代码。不要在main
中声明名为cloud
的局部变量.Havemain
改为赋值给全局变量。如前所述,全局变量cloud
为空-它不指向任何对象;这就是为什么您在外部看到崩溃的原因,它崩溃是因为变量为空。它为空是因为您从未赋值给它。相反,您赋值给另一个变量,这只是使用相同名称的笔。这并不是共享指针特有的;您使用shared\u ptr
这一事实是无关紧要的。我不知道如何更清楚地解释它。
//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();
#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