Skip to content

[registration] Sporadic segfault in NDT after repeated align #5392

@sadsimulation

Description

@sadsimulation

Describe the bug

After calling align on an NormalDistributionsTransform object, I get a segfault somewhere deep inside the call stack (see below).

Context

I'm trying to match successive point clouds using the NDT. The NDT is used in the callback function of a ROS service. Every time the callback function executes, I instantiate a NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ>, set the source and target point clouds and execute the align method. This seems to work the first four or five times but then suddenly I get a segfault and the node crashes.

Expected behavior

NDT shouldn't segfault, especially because it seems to work for a while before the issue occurs.

Current Behavior

Repeated calls to NDT cause segfault somewhere deep inside the call stack.

To Reproduce

See code below, it's rather straightforward, just repeatedly call NDT.

Screenshots/Code snippets

#include "ros/ros.h"

#include "pcl/PCLPointCloud2.h"
#include "pcl/point_types.h"
#include "pcl/registration/ndt.h"
#include "pcl/registration/icp.h"

#include "pcl_conversions/pcl_conversions.h"
#include "point_types.h"

#include "tf2_eigen/tf2_eigen.h"

#include "myslam_msgs/PointCloudScanMatcher.h"

namespace myslam
{

class ScanMatcher
{
    private:
        double termination_threshold, optimization_step_size, grid_resolution;
        unsigned int max_iterations;

    public:
        ScanMatcher(double termination_threshold, unsigned int max_iterations, double optimization_step_size, double grid_resolution)
            : termination_threshold(termination_threshold), max_iterations(max_iterations), optimization_step_size(optimization_step_size), grid_resolution(grid_resolution) {}

        bool match(myslam_msgs::PointCloudScanMatcher::Request &req, myslam_msgs::PointCloudScanMatcher::Response &res)
        {
            // ROS interface
            // convert ROS PointCloud2 messages to CustomCloud (PointCloud<CustomPoint>)
            CustomCloud::Ptr reference(new CustomCloud);
            CustomCloud::Ptr measurement(new CustomCloud);
            pcl::fromROSMsg(req.reference, *reference);
            pcl::fromROSMsg(req.measurement, *measurement);
            // convert ROS Transform to Eigen::Matrix4f
            Eigen::Matrix4f initial_guess = tf2::transformToEigen(req.transform).matrix().cast<float>();
            // PCL interface
            pcl::PointCloud<pcl::PointXYZ>::Ptr reference_xyz(new pcl::PointCloud<pcl::PointXYZ>);
            pcl::PointCloud<pcl::PointXYZ>::Ptr measurement_xyz(new pcl::PointCloud<pcl::PointXYZ>);
            pcl::copyPointCloud(*reference, *reference_xyz);
            pcl::copyPointCloud(*measurement, *measurement_xyz);

            // // crashes
            pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> reg;
            reg.setTransformationEpsilon(termination_threshold);
            reg.setMaximumIterations(max_iterations);
            reg.setStepSize(optimization_step_size);
            reg.setResolution(grid_resolution);

            // the input cloud which will be aligned
            reg.setInputSource(reference_xyz);
            // the reference pointcloud to align to
            reg.setInputTarget(measurement_xyz);
            // output dummy cloud (unused)
            pcl::PointCloud<pcl::PointXYZ>::Ptr output_cloud(new pcl::PointCloud<pcl::PointXYZ>);
            // perform alignment
            reg.align(*output_cloud, initial_guess);
            // extract transformation
            Eigen::Affine3d final_transform;
            final_transform.matrix() = reg.getFinalTransformation().cast<double>();
            // ROS interface
            // convert to ROS message
            res.transform = tf2::eigenToTransform(final_transform).transform;
            res.valid = reg.hasConverged();
            return true;
        }
};

} // namespace myslam

int main(int argc, char **argv)
{
    ros::init(argc, argv, "pointcloud_scanmatcher_server");
    ros::NodeHandle nh;
    ros::NodeHandle nh_private("~");

    double termination_threshold;
    double optimization_step_size;
    int max_iterations;
    double grid_resolution;

    nh_private.param("termination_threshold", termination_threshold, 0.01);
    nh_private.param("optimization_step_size", optimization_step_size, 0.1);
    nh_private.param("max_iterations", max_iterations, 30);
    nh_private.param("grid_resolution", grid_resolution, 0.5);

    myslam::ScanMatcher matcher(termination_threshold, optimization_step_size, (unsigned int)max_iterations, grid_resolution);

    ros::ServiceServer service = nh.advertiseService("pointcloud_scanmatcher", &myslam::ScanMatcher::match, &matcher);

    ROS_INFO("Ready to match pointclouds");
    while(ros::ok()) {
        ros::spin();
    }
    return 0;
}
==173658== Invalid write of size 4
==173658==    at 0x491C969: pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::radiusSearch(pcl::PointXYZ const&, double, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&, unsigned int) const (in /home/riy2rng/.conda/envs/mamba/envs/myenv/lib/libpcl_kdtree.so.1.12.0)
==173658==    by 0x2EF420: pcl::VoxelGridCovariance<pcl::PointXYZ>::radiusSearch(pcl::PointXYZ const&, double, std::vector<pcl::VoxelGridCovariance<pcl::PointXYZ>::Leaf const*, std::allocator<pcl::VoxelGridCovariance<pcl::PointXYZ>::Leaf const*> >&, std::vector<float, std::allocator<float> >&, unsigned int) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2EB074: pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ>::computeDerivatives(Eigen::Matrix<double, 6, 1, 0, 6, 1>&, Eigen::Matrix<double, 6, 6, 0, 6, 6>&, pcl::PointCloud<pcl::PointXYZ> const&, Eigen::Matrix<double, 6, 1, 0, 6, 1> const&, bool) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E8B42: pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ>::computeTransformation(pcl::PointCloud<pcl::PointXYZ>&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2C57DA: pcl::Registration<pcl::PointXYZ, pcl::PointXYZ, float>::align(pcl::PointCloud<pcl::PointXYZ>&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2C22FC: myslam::ScanMatcher::match(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E05DF: boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::operator()(myslam::ScanMatcher*, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2DD571: bool boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> >::operator()<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::rrlist2<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&> >(boost::_bi::type<bool>, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>&, boost::_bi::rrlist2<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>&, long) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2DA6D0: bool boost::_bi::bind_t<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> > >::operator()<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2D73D5: boost::detail::function::function_obj_invoker2<boost::_bi::bind_t<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> > >, bool, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::invoke(boost::detail::function::function_buffer&, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2EC884: boost::function2<bool, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::operator()(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E9CEC: ros::ServiceSpec<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> > >::call(boost::function<bool (myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&)> const&, ros::ServiceSpecCallParams<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> > >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==173658== 
==173658== 
==173658== Process terminating with default action of signal 11 (SIGSEGV)
==173658==  Access not within mapped region at address 0x0
==173658==    at 0x491C969: pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::radiusSearch(pcl::PointXYZ const&, double, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&, unsigned int) const (in /home/riy2rng/.conda/envs/mamba/envs/myenv/lib/libpcl_kdtree.so.1.12.0)
==173658==    by 0x2EF420: pcl::VoxelGridCovariance<pcl::PointXYZ>::radiusSearch(pcl::PointXYZ const&, double, std::vector<pcl::VoxelGridCovariance<pcl::PointXYZ>::Leaf const*, std::allocator<pcl::VoxelGridCovariance<pcl::PointXYZ>::Leaf const*> >&, std::vector<float, std::allocator<float> >&, unsigned int) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2EB074: pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ>::computeDerivatives(Eigen::Matrix<double, 6, 1, 0, 6, 1>&, Eigen::Matrix<double, 6, 6, 0, 6, 6>&, pcl::PointCloud<pcl::PointXYZ> const&, Eigen::Matrix<double, 6, 1, 0, 6, 1> const&, bool) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E8B42: pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ>::computeTransformation(pcl::PointCloud<pcl::PointXYZ>&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2C57DA: pcl::Registration<pcl::PointXYZ, pcl::PointXYZ, float>::align(pcl::PointCloud<pcl::PointXYZ>&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2C22FC: myslam::ScanMatcher::match(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E05DF: boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::operator()(myslam::ScanMatcher*, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2DD571: bool boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> >::operator()<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::rrlist2<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&> >(boost::_bi::type<bool>, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>&, boost::_bi::rrlist2<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>&, long) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2DA6D0: bool boost::_bi::bind_t<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> > >::operator()<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2D73D5: boost::detail::function::function_obj_invoker2<boost::_bi::bind_t<bool, boost::_mfi::mf2<bool, myslam::ScanMatcher, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>, boost::_bi::list3<boost::_bi::value<myslam::ScanMatcher*>, boost::arg<1>, boost::arg<2> > >, bool, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::invoke(boost::detail::function::function_buffer&, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2EC884: boost::function2<bool, myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&>::operator()(myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&) const (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==    by 0x2E9CEC: ros::ServiceSpec<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> > >::call(boost::function<bool (myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >&, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> >&)> const&, ros::ServiceSpecCallParams<myslam_msgs::PointCloudScanMatcherRequest_<std::allocator<void> >, myslam_msgs::PointCloudScanMatcherResponse_<std::allocator<void> > >&) (in /home/riy2rng/Documents/ros/myslam_catkin_ws/devel/lib/myslam/ScanMatcher)
==173658==  If you believe this happened as a result of a stack
==173658==  overflow in your program's main thread (unlikely but
==173658==  possible), you can try to increase the size of the
==173658==  main thread stack using the --main-stacksize= flag.
==173658==  The main thread stack size used in this run was 8388608.
==173658== 
==173658== HEAP SUMMARY:
==173658==     in use at exit: 298,107 bytes in 939 blocks
==173658==   total heap usage: 25,701 allocs, 24,762 frees, 45,506,667 bytes allocated
==173658== 
==173658== LEAK SUMMARY:
==173658==    definitely lost: 0 bytes in 0 blocks
==173658==    indirectly lost: 0 bytes in 0 blocks
==173658==      possibly lost: 66,752 bytes in 12 blocks
==173658==    still reachable: 231,355 bytes in 927 blocks
==173658==         suppressed: 0 bytes in 0 blocks
==173658== Rerun with --leak-check=full to see details of leaked memory
==173658== 
==173658== For lists of detected and suppressed errors, rerun with: -s
==173658== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

Your Environment (please complete the following information):

  • OS: Ubuntu 20.04
  • Compiler: GCC 9.4.0
  • PCL Version 1.12.0
  • ROS noetic

Possible Solution

Sorry, no idea

Additional context

There seems to be a similar bug #4944, but I'm not sure I understand their solution.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions