对主作用域之外的boost:shared_ptr的访问因断言失败而崩溃:px!=0。指针的正确用法是什么?

时间:2018-09-11 17:40:06

标签: c++ boost point-cloud-library

对访问类型为boost:shared_ptr的云的访问因断言失败而崩溃:px!= 0在main外部错误,但是在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?
}

2 个答案:

答案 0 :(得分:0)

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

因此,全局变量在main内部不可见,并且未使用,直到您调用outside为止,然后它仍指未使用的全局变量。

答案 1 :(得分:0)

如@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