Search code examples
c++c++11rospoint-cloud-library

templated class faults after running in C++11


I am developing a ROS package using PCL.

The problem am getting is that it compiles successfully with C++98 and C++11. But, when the node is launched in the C++11 supported version (CMakeLists L:5, not commented), it segfaults right away, although, it works fine when compiled without C++11.

The same code architecture (templated library) of this package shows the same behavior.

I came to find that the templated library is the one holding the reason why. But, I couldn't figure it out yet. what am I missing here ?

Platform : Ubuntu 14.04, ROS Indigo, GCC 4.8.4.

Thanks.

node code :

#include <ros/ros.h>
#include "pcl_tools.h"
// PCL specific includes
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
using namespace pcl_tools;
ros::Publisher pub;

void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
  /// ros to pcl cloud
  pcl::PCLPointCloud2 cloud2_in;
  pcl_conversions::toPCL(*input,cloud2_in);
  pcl::PointCloud< pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud< pcl::PointXYZ>);
  pcl::fromPCLPointCloud2(cloud2_in,*temp_cloud);

  /// use templated function
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
  cloud_filtered = passthroughfilter<pcl::PointXYZ>(temp_cloud, "y", -2, 2.0);
}

int main (int argc, char** argv)
{
  ros::init (argc, argv, "template_test");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe ("/camera/depth/points", 1, cloud_cb);
  ros::spin ();
}

template header :

#ifndef PCL_TOOLS
#define PCL_TOOLS

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <string>
#include <sstream>
#include <assert.h>

namespace pcl_tools
{
    template <typename pointT> boost::shared_ptr< pcl::PointCloud<pointT> >
    passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> >, std::string, float, float, bool invert = false);
}

template <typename pointT>
boost::shared_ptr< pcl::PointCloud<pointT> > pcl_tools::passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> > input,
                                                                std::string axis, float min , float max, bool invert = false)
{
    typedef boost::shared_ptr< pcl::PointCloud<pointT> > PointCloudPtr;
    PointCloudPtr cloud(new pcl::PointCloud<pointT>);

    // Create the filtering object
    pcl::PassThrough<pointT> pass;
    pass.setInputCloud (input);
    pass.setFilterFieldName (axis);
    pass.setFilterLimits (min, max);
    pass.setFilterLimitsNegative (invert);
    pass.filter (*cloud);
    return cloud;
}
#endif

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(test_template)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  geometry_msgs
  image_transport
  pcl_conversions
  pcl_ros
  roscpp
  rospy
  sensor_msgs
  std_msgs
)

add_definitions(${PCL_DEFINITIONS})
catkin_package()

include_directories(
    include/${PROJECT_NAME}
    ${catkin_INCLUDE_DIRS}
    ${PCL_INCLUDE_DIRS}
)

link_directories(${PCL_LIBRARY_DIRS})

add_executable(pcl_template_test src/pcl_template.cpp)
target_link_libraries(pcl_template_test ${catkin_LIBRARIES} )

Solution

  • Well, it turns out that this was thrown by PCL library. In fact, installing PCL through apt-get installs the non C++11 compatible version. So, to overcome that I had to recompile it from source, adding the C++11 support. More on this issue here.