問題解決:shared_ptr Assertion px != 0 failed 及debug經驗分享

Kadima_du發表於2020-10-03

問題解決:shared_ptr Assertion px != 0 failed及debug經驗分享

問題詳細描述:

/usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference::type boost::shared_ptr::operator*() const [with T = pcl::PointCloudpcl::pointxyz; typename boost::detail::sp_dereference::type = pcl::PointCloudpcl::pointxyz&]: Assertion `px != 0’ failed. Aborted (core dumped)

問題原因:

在使用boost::shared_ptr之前未進行初始化或者未正確初始化

錯誤程式碼示例:

class Listener
{

public:
	#在這裡使用cloud_passthrough時未正確初始化
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_passthrough;

    void callback(const sensor_msgs::PointCloud2ConstPtr& kinect_output);

};
// end class

void Listener::callback(const sensor_msgs::PointCloud2ConstPtr& kinect_output)
{
    pcl::PCLPointCloud2::Ptr pc2 (new pcl::PCLPointCloud2());

    pcl_conversions::toPCL (*kinect_output, *pc2);

    pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());

    pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
    sor.setInputCloud (pc2);
    sor.setLeafSize(0.01f,0.01f,0.01f);
    sor.filter(*cloud_filtered);// filter the point cloud
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloudXYZ (new pcl::PointCloud<pcl::PointXYZ>);//object cloud for point cloud type XYZ
    pcl::fromPCLPointCloud2(*cloud_filtered,*cloudXYZ);

    //create passthrough filter object
    pcl::PassThrough<pcl::PointXYZ> pass;
    pass.setInputCloud (cloudXYZ);
    pass.setFilterFieldName("z");
    pass.setFilterLimits(0.5, 1.5);
    pass.filter(*cloud_passthrough);
}

int main (int argc, char **argv)
{
    ros::init (argc, argv, "PointCloud2_to_pointCloudXYZ");

    ros::NodeHandle nh;

    Listener listener;

    ros::Publisher pub = nh.advertise<pcl::PointCloud<pcl::PointXYZ> >("/filtered", 1000);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    cloud -> points = listener.cloud_passthrough->points;
    ros::Rate loop_rate(10);    
    ros::Subscriber sub = nh.subscribe <sensor_msgs::PointCloud2> ("/camera/depth/points",1000,&Listener::callback, &listener);
    while (ros::ok())
    {
        pub.publish (cloud);
        ros::spinOnce();
        loop_rate.sleep();
    }//end while loop   
    return 0;
}//end main

正確初始化示例:

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_passthrough(new pcl::PointCloud<pcl::PointXYZ>);
boost::shared_ptr<Obj> obj;
obj->Something(); // assertion failed

boost::shared_ptr<Obj> obj(new Obj);
obj->Something(); // ok

最後分享一個debug的經驗,能夠使用科學上網的童鞋,可以Google搜尋stack overflow網站,針對自己遇到的問題進行搜尋,一般情況下均可以搜到對應的解決方法(ps:回答前帶有綠色對號的基本為經過人們驗證的正確解決方案)。

相關文章