关于c ++:解除分配pcl :: PointCloud :: Ptr时的分段错误

Segmentation fault when deallocating pcl::PointCloud::Ptr

我有一个可以成功读取Pointcloud并将其存储在pcl::PointCloud::Ptr pcd中的函数

然后我跑

1
2
//filter the pointcloud to remove some noise while still keeping the cloud dense
pcl::PointCloud<pcl::PointXYZ>::Ptr tmp = filter_obj.filterVoxelGrid(pcd, 0.01, 0.01, 0.01);

其中filter_objstereo_pointcloud_filter的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pcl::PointCloud<pcl::PointXYZ>::Ptr stereo_pointcloud_filter::filterVoxelGrid(
    pcl::PointCloud<pcl::PointXYZ>::Ptr inputcloud,
    float voxelX, float voxelY, float voxelZ)
{

    pcl::PointCloud<pcl::PointXYZ>::Ptr outputcloud(new pcl::PointCloud<pcl::PointXYZ>);

    pcl::VoxelGrid<pcl::PointXYZ> sor;
    sor.setInputCloud(inputcloud);
    sor.setLeafSize(voxelX, voxelY, voxelZ);
    sor.filter(*outputcloud);

    pcl::PointCloud<pcl::PointXYZ>::Ptr result(outputcloud);
    return result;
}

tmp的解除分配期间出现分段错误。 我几乎可以肯定,该错误与filterVoxelGrid()中的一些错误指针有关,但我不确定如何解决。

这是调用堆栈

libc.so.6!__GI___libc_free(void * mem) (/usr/src/glibc/glibc-2.23/malloc/malloc.c:2951)
Eigen::internal::handmade_aligned_free(void * ptr) (/home/shawn/Documents/Projects/catkin_ws/devel/include/Eigen/src/Core/util/Memory.h:98)
Eigen::internal::aligned_free(void * ptr) (/home/shawn/Documents/Projects/catkin_ws/devel/include/Eigen/src/Core/util/Memory.h:179)
Eigen::aligned_allocator::deallocate(Eigen::aligned_allocator * const this, Eigen::aligned_allocator::pointer p) (/home/shawn/Documents/Projects/catkin_ws/devel/include/Eigen/src/Core/util/Memory.h:755)
std::allocator_traits >::deallocate(Eigen::aligned_allocator & __a, std::allocator_traits >::pointer __p, std::allocator_traits >::size_type __n) (/usr/include/c++/5/bits/alloc_traits.h:386)
std::_Vector_base >::_M_deallocate(std::_Vector_base > * const this, std::_Vector_base >::pointer __p, std::size_t __n) (/usr/include/c++/5/bits/stl_vector.h:178)
std::_Vector_base >::~_Vector_base(std::_Vector_base > * const this) (/usr/include/c++/5/bits/stl_vector.h:160)
std::vector >::~vector(std::vector > * const this) (/usr/include/c++/5/bits/stl_vector.h:425)
pcl::PointCloud::~PointCloud(pcl::PointCloud * const this) (/usr/local/include/pcl-1.8/pcl/point_cloud.h:240)
pcl::PointCloud::~PointCloud(pcl::PointCloud * const this) (/usr/local/include/pcl-1.8/pcl/point_cloud.h:240)
boost::checked_delete >(pcl::PointCloud * x) (/usr/include/boost/core/checked_delete.hpp:34)
boost::detail::sp_counted_impl_p >::dispose(boost::detail::sp_counted_impl_p > * const this) (/usr/include/boost/smart_ptr/detail/sp_counted_impl.hpp:78)
boost::detail::sp_counted_base::release(boost::detail::sp_counted_base * const this) (/usr/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:146)
boost::detail::shared_count::~shared_count(boost::detail::shared_count * const this) (/usr/include/boost/smart_ptr/detail/shared_count.hpp:443)
boost::shared_ptr >::~shared_ptr(boost::shared_ptr > * const this) (/usr/include/boost/smart_ptr/shared_ptr.hpp:323)
read_PCD_file(std::__cxx11::string pcdFilePath) (/home/shawn/Documents/Projects/catkin_ws/src/file.cpp:402)
main(int argc, char ** argv) (/home/shawn/Documents/Projects/catkin_ws/src/file.cpp:567)


问题出在PCL库中。 我的计算机上有一些不同版本的PCL生成,这可能会引起某种冲突。 清除所有内容并重新开始可以清除此错误。


虽然我找不到解决此问题的方法,但我找到了解决方法。 我切换为使用pcl::PointCloudpcl::PCLPointCloud2 intead,并且代码工作正常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pcl::PointCloud<pcl::PointXYZ>::Ptr stereo_pointcloud_filter::filterVoxelGrid(
    pcl::PointCloud<pcl::PointXYZ>::Ptr inputcloud,
    float voxelX, float voxelY, float voxelZ)
{
    pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
    pcl::toPCLPointCloud2(*inputcloud, *cloud);
    pcl::PointCloud<pcl::PointXYZ>::Ptr outputcloud(new pcl::PointCloud<pcl::PointXYZ>);

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

    // Create the filtering object
    pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
    sor.setInputCloud(cloud);
    sor.setLeafSize(voxelX, voxelY, voxelZ);
    sor.filter(*cloud_filtered);

    pcl::fromPCLPointCloud2(*cloud_filtered, *outputcloud);

    return outputcloud;
}