ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Cleaning dynamic memory allocation in a call back function.

asked 2018-04-02 14:47:26 -0500

BhanuKiran.Chaluvadi gravatar image

updated 2018-04-02 14:48:03 -0500

Hi, My callback function looks like this.

void  cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input_pc)
  // Convert the sensor_msgs/PointCloud2 data to pcl/PointCloud - Dataset
  pcl::PointCloud<PointT>::Ptr cloud_in(new pcl::PointCloud<PointT>);
  pcl::fromROSMsg(*input, *cloud_in);
  // pcl::norms
  pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
  for(auto searchPoint: filtered_cloud->points)
    pcl::PCA<PointT> local_pca = new pcl::PCA<PointT>;
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
   delete local_pca;
   delete inliers;
// delete cloud;
// delete cloud_normals;

Is it a good practice to de-allocate memory for the variables "cloud" , "cloud_normals" after the end of callback function ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-02 15:32:37 -0500

lucasw gravatar image

updated 2018-04-02 15:33:42 -0500

The pcl Ptrs are boost shared_ptrs and clean up after themselves (you should look this up elsewhere, line 428 for example), but local_pca is raw allocated memory so you need to clean that up yourself after you finish using it (like the end of the callback) unless somewhere in the ... you are handing it to a constructor or something else that takes ownership of the memory.

This isn't really ros related except you happen to be in a callback and converting to pcl from a sensor_msg, the memory allocation is purely the domain of pcl/boost/C++.

edit flag offensive delete link more

Question Tools


Asked: 2018-04-02 14:47:26 -0500

Seen: 717 times

Last updated: Apr 02 '18