Like Tree1Likes
  • 1 Post By anon

Do I need to delete dynamic memory before leaving a function

This is a discussion on Do I need to delete dynamic memory before leaving a function within the C++ Programming forums, part of the General Programming Boards category; Ok, I'm pretty sure that I'll get my head bitten of for this but if you are going to, please ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    17

    Do I need to delete dynamic memory before leaving a function

    Ok, I'm pretty sure that I'll get my head bitten of for this but if you are going to, please at least point me to a webpage that explains it.

    I am using Point Cloud Library to manipulate point clouds (just to give the context). From my main I call a function and withing that function I use
    Code:
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudIn(new pcl::PointCloud<pcl::PointXYZRGB>());
    It is essentially a class that acts as a pointer and the constructor needs the regular class as its argument. My question is this: do I need to delete the new pcl::PointCloud<pcl::PointXYZRGB> object before I leave the function or will it delete this by itself? I can run it just fine without doing so but I'm not sure if this causes memory leaks (I've encountered a lot of articles about this on the internet)

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    If you're sure that the library won't require the abject later on, deleting it is fine.
    You could also use smart pointers...which will automatically destroy it when not needed.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    17
    I just found out experimentally that if I try to delete it just before I leave the function, I get a segmentation fault. This fault happens after the last line of the function and before the first line the main after calling the function. It seems that I cannot delete it without problems but that would seem to contradict what i have read about dynamically allocated memory and deleting.

    @manasij7479 What is a smart pointer? How would you implement one?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by njitram2000 View Post
    I just found out experimentally that if I try to delete it just before I leave the function, I get a segmentation fault. This fault happens after the last line of the function and before the first line the main after calling the function. It seems that I cannot delete it without problems but that would seem to contradict what i have read about dynamically allocated memory and deleting.
    That just means that it is needed afterwards.
    @manasij7479 What is a smart pointer? How would you implement one?
    I've never implemented one...but a popular way is using reference counting.(Google the term)
    But unless you're sure that you can do better, stick with the ones provided by the C++ standard.
    Smart pointer - Wikipedia, the free encyclopedia
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr is a typedef for boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB > >. It already is a smart pointer, which means you needn't and must not try to delete it yourself. The object will be automatically deallocated when the (last) shared_ptr to this resource goes out of scope.

    Consult the manual: http://docs.pointclouds.org/trunk/classpcl_1_1_point_cloud.html
    Last edited by anon; 12-09-2011 at 09:13 AM.
    rags_to_riches likes this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Always when you pass a raw pointer to some function or object, consult the manual! It should say if you need to delete it or not.
    (If it doesn't, then complain about the documentation being poor.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    17
    @anon & Elysia - Thank you, that explains everything. I looked through the PCL manual but not the Boost one (seemed a bit too daunting )
    I did that now and I'll remember that tip for in the future.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    All you need to know is that if it it is dynamically allocated then it needs to be deleted by something, somewhere, at some point during the program execution.

    Whether it is your code that does that or if it's part of what the library you're using does for you, is up to you to find out.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete an element in a dynamic array?
    By beta3designs in forum C Programming
    Replies: 34
    Last Post: 08-22-2011, 03:34 AM
  2. Dynamic memory allocation for structs passed to function
    By eXcellion in forum C Programming
    Replies: 18
    Last Post: 03-30-2011, 03:30 PM
  3. Pointer/Dynamic Memory/function problem
    By Artist_of_dream in forum C++ Programming
    Replies: 17
    Last Post: 12-26-2004, 04:57 PM
  4. dynamic memory deletion via function
    By starkhorn in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2004, 09:11 AM
  5. delete dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 1
    Last Post: 07-18-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21