Preventing memory leaks - delete function...

Started by
3 comments, last by hh10k 15 years, 5 months ago
I want to make sure when I do something in VC++ that there are no memory leaks, please help me understand some of the programming concepts. If the variable or instance of the class is created withing the function, is the reserved memory free from it after the function ends? What needs to be deleted explicitly before program ends? What is automatically deleted? When do I need to use Delete function? Is there a way to see if the program leeks? Do you have some good tip about this subject, or something I did not ask but is important regarding this subject? Thanks
Advertisement
If you allocate an object with new, you have to delete it or pass it to something that will delete it for you. The same goes for new[] and delete[].

Nothing has to be deleted before the program ends, as the operating system usually cleans up. However, code which waits for the program to end before cleaning up is code that slows down the program until it ends, so avoid it.

As a rule of thumb, avoid using new yourself. If you do, avoid using delete yourself.
Quote:If the variable or instance of the class is created withing the function, is the reserved memory free from it after the function ends?

If the variable or instance was created on the stack, yes. If the heap, no. (In the case of the heap: The pointer itself will be destroyed, but the data it points to that has been allocated will not).
Quote:What needs to be deleted explicitly before program ends?

As ToohrVyk stated, nothing is actually "needed" to be explicitly deleted at program end but it is recommended to free any memory that your program has allocated before program end.
Quote:What is automatically deleted?

Assuming you mean the delete keyword, and are using unmanaged C++ (Although I do not know if this applies to managed C++ as well), then the only allocations that are automatically deleted are those that are deleted within object dtors. (However this is only because dtors are called when an object is destroyed. The actual deallocation is not automatic)
Quote:When do I need to use Delete function?

As early as you can on an object that has been allocated via the new keyword.
Quote:Is there a way to see if the program leeks?

There are several methods. ie, if the task manager (Windows) memory usage of the program slowly continually increases, it may be a sign of a memory leak somewhere in your code. There are also memory managers that you can use.
For memory leak checking i recommend Valgrind

Also i would use reference counting if i were you to see when to delete an object.
Just hold an integer storing the number of classes or whatever that use an object at the moment. if this integer is set to 0 delete the object.
My Blog - http://www.freakybytes.org
Quote:Original post by ToohrVyk
Nothing has to be deleted before the program ends, as the operating system usually cleans up.


That's true if it's only memory, however things like files won't properly flush their buffer to disk unless the destructor is called.

Normally you rarely call delete yourself, because it's very easy to leak resources when exceptions are thrown. You should look into using techniques like RAII. The Boost Smart Pointers are a popular set of classes that help you with this.

This topic is closed to new replies.

Advertisement