0

I am working through some PCL tutorials. In most tutorials the memory for the clouds is allocated dynamically, e.g.:

pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);

However, when I try to collect the garbage at the end of the function I get an error message:

error: cannot delete expression of type 'pcl::search::KdTree::Ptr' (aka 'shared_ptr > > >') delete tree;

My confusion boils down to this:

  1. Am I misinterpreting the declaration of the tree variable?
  2. What is the meaning of the <type*> <variableName> (new <type>) expression? Does this not create a pointer and allocate new memory on the heap?
  3. How would I go about collecting the garbage correctly?

Maybe somebody can shed some light onto this for me. Many thanks in advance!

budekatude
  • 433
  • 6
  • 24
  • 1
    This looks like a smart pointer. You may not delete contents of a smart pointer - it's deleted automatically if last (sharing) smart pointer is destroyed (i.e. goes out of scope). – Scheff's Cat Aug 08 '18 at 09:12
  • Have a look at [pcl::search::KdTree< PointT > Class Template Reference](http://docs.pointclouds.org/1.6.0/classpcl_1_1search_1_1_kd_tree.html). `Ptr` is typedef'ed as [`boost::shared_ptr`](https://www.boost.org/doc/libs/1_62_0/libs/smart_ptr/shared_ptr.htm). – Scheff's Cat Aug 08 '18 at 09:16
  • 1
    Brilliant! Thanks very much! I didn't know smart pointers existed! But the link you posted in the previous comment clarified it all! If you want to post it as an answer I will mark it as the correct one! Thanks so much! – budekatude Aug 08 '18 at 09:20
  • 1
    Thanks for the offer but smart pointer are something essential in modern programming (not only in C++). Thus, I had wondered if there were never published something about them in SO. I found [SO: What is a smart pointer and when should I use one?](https://stackoverflow.com/q/106508/7478597). I guess this is sufficient but I'm happy that I could help. ;-) – Scheff's Cat Aug 08 '18 at 09:23

0 Answers0