访问提升: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?
访问类型为 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
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?