3

I have a function that reads in a pointcloud successfully and stores it in pcl::PointCloud<pcl::PointXYZ>::Ptr pcd

I then run

//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);

where filter_obj is an object of 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;
}

I get a segmentation fault during de-allocation of tmp. I'm almost certain the error is to do with some bad pointers in filterVoxelGrid(), but I'm not sure how to solve it.

Here's the call stack

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)

6
  • 2
    When asking a question about an error you encounter, please always include the verbatim text of the error message in your question. Commented Apr 17, 2020 at 18:26
  • You could have code that clobbers memory after tmp is created but before it is destroyed, and it is only during the destruction of tmp that the runtime notices the problem. Commented Apr 17, 2020 at 18:27
  • If this is linux code, it might be worth running it through valgrind, this could give some insight into how the error occurs. Commented Apr 17, 2020 at 18:37
  • @JesperJuhl I've added the stack trace. I do not get an error message. Commented Apr 17, 2020 at 18:57
  • 1
    in recent times i pushed a few fixes to pcl fixing memory alignment problems in the filters module. It might be that VoxelGrid header was missing the PCL_MAKE_ALIGNED_OPERATOR_NEW. Commented Dec 9, 2022 at 13:24

2 Answers 2

4

While I wasn't able to find a solution to this problem, I found a workaround. I switched to using pcl::PCLPointCloud2 intead of pcl::PointCloud<pcl::PointXYZ> and the code works fine.

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It would be nice to know what the root cause of this issue.
0

The problem lay somewhere in the PCL libraries. I had a few builds of different versions of PCL on my machine which probably caused some sort of conflict. Wiping everything and starting again cleared this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.