当释放 pcl::PointCloud<pcl::PointXYZ>::Ptr 时出现分段错误

3

我有一个函数可以成功读取点云,并将其存储在pcl::PointCloud<pcl::PointXYZ>::Ptr pcd中。

然后我运行

//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_obj是一个stereo_pointcloud_filter对象

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)

这段代码涉及到的是内存释放,其中包括了一些C++库和第三方库的函数。如果你在使用这些库时出现了内存问题,那么这段代码可能会对你有所帮助。

2
当询问您遇到的错误时,请务必在问题中始终包含错误消息的逐字逐句的文字。 - Jesper Juhl
你可能会有一个在tmp创建后但销毁前破坏内存的代码,只有在销毁tmp时,运行时才会注意到这个问题。 - 1201ProgramAlarm
如果这是Linux代码,值得通过valgrind运行它,这可能会揭示错误发生的原因。 - Owl
@JesperJuhl 我已经添加了堆栈跟踪。我没有收到错误消息。 - Shawn Mathew
@Owl 我会尝试使用valgrind进一步追踪问题 - Shawn Mathew
1
最近,我对PCL进行了一些修复,解决了滤波模块中的内存对齐问题。可能是由于VoxelGrid头文件缺少PCL_MAKE_ALIGNED_OPERATOR_NEW导致的。 - oarfish
2个回答

3

虽然我没有找到解决这个问题的方法,但我找到了一个解决办法。我改用pcl::PCLPointCloud2而不是pcl::PointCloud<pcl::PointXYZ>,代码就可以正常工作。

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;
}

知道这个问题的根本原因会很好。 - rocklegend

0
问题出在PCL库的某个地方。我的机器上安装了几个不同版本的PCL,可能导致了某种冲突。清除所有内容并重新开始解决了这个错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接