# How to clone a PointCloud in a subscriber callback

Dear all, I am using Indigo, coming from Groovy and Hydro, I found it difficult to understand the internals of pcl_ros and pcl_conversions packages.

My problem is the following: I have a subscription callback like this: void RosHandler::xtionCloudCallback(const sensor_msgs::PointCloud2::ConstPtr& msg); or, for me it's the same, like this: void RosHandler::xtionCloudCallback(const pcl::PointCloud<pcl::pointxyzrgb>::ConstPtr& msg);

the first question is: is this pcl::PointCloud<pcl::pointxyzrgb> the same as in pcl? if it is the same, how can I clone the data, in such a way that I can use them without bothering of what ROS is doing meanwhile (i.e., in another thread)?

What I did in Groovy (and it worked) was to manually copy the message field by field (and cloning "data"), but it does not work anymore (and, indeed, I was not sure it was safe to do that in that way).

EDIT: when I say "it does not work" I mean that it segfault when I delete the new (cloned) cloud, so this means the ownership of the cloud was not (entirely?) mine EDIT2: the problem is not only I cannot delete the cloud, of course, but also that I cannot read it (it has been deleted by someone else)

edit retag close merge delete

Sort by » oldest newest most voted

just to expand on @paulbovbel 's answer

If you want to continue with processing shared_ptrs maybe the makeShared() method helps you. Creates a deep copy of the cloud and returns you a shared_ptr to the copied cloud, see:

http://docs.pointclouds.org/trunk/cla...

more

Yes it is the same object as in PCL, so the same APIs will work. pcl::Poincloud has a copy constructor that you can use to make the copy (pcl::Poincloud copy = pcl::PointCloud(*cloud_msg_const_ptr))

Addendum, the message you receive is const, so you can feel safe using the received cloud directly without copying, as long as you don't need/try to modify it.

more

Actually, I temporary solved the problem,by cloning the vector<pointxyzrgb> (that I know how to deep-clone), and it works without segfaults. It means that during the copy (I tested with the copy constructor and with the copy field by field) there's something in the header that is not copied properly

( 2014-09-19 05:21:36 -0600 )edit

Either copy constructor or @Wolf's answer (makeShared()) should work to make a deep copy the original object.

( 2014-09-19 10:59:29 -0600 )edit

I'm not really sure what you mean by cloning, but for modern C++ with std and boost libraries and RAII, there's not many occasions when you should be calling delete on anything, ever. If you want to post a gist of your code, I could tell you more.

( 2014-09-19 11:00:34 -0600 )edit