tags:

views:

787

answers:

8

In my C++ main function, for example, if I had a pointer to a variable which uses heap memory (as opposed to stack memory) - is this automatically deallocated after my application exits? I would assume so.

Even so, is it good practice to always delete heap allocations even if you think they will never be used in a situation where the memory is automatically deallocated on exit?

For example, is there any point in doing this?

int main(...)
{
    A a = new A();
    a->DoSomething();
    delete a;
    return 0;
}

I was thinking maybe in case I refactor (or someone else refactors) that code and puts it elsewhere in the application, where delete is really neccecary.

Edit:

As well as the answer by Brian R. Bondy (which talks specifically about the implications in C++), Paul Tomblin also has a good answer to a C specific question, which also talks about the C++ destructor.

+2  A: 

This is a near duplicate if not a duplicate of http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc

Paul Tomblin
Ah, didn't spot that, should I delete my question?
nbolton
@Nick, probably a good idea.
Paul Tomblin
I guess this is the C++ equivalent... maybe it would be useful to someone?
nbolton
@Nick: nah, just make sure to put an honest effort into finding questions which are related to the one you're asking first.
Welbog
Well, the answer is pretty much the same for any language. I don't see a point in having two questions attracting the same answers.
Paul Tomblin
@Nick Bolton: Yes there is a big difference in the answer because it is C++. There is insightful information given about the destructor in my answer. Please change your subject to explicitly say C++.
Brian R. Bondy
@Welbog: I looked at the suggestions but none seemed to be relevant; were you saying "nah, this would be useful to someone" or "nah, delete it"?
nbolton
@Nick: I was saying "nah, don't delete it." As long as you say you looked around before you posted, I say your post is good. But if I find out you're lying I'm going to find your address and mail you an extra Sears catalogue. The mailman will never forgive you.
Welbog
Haha, I remember that... Who did it happen to originally? Funny joke.
nbolton
@Paul Tomblin: Maybe link directly to your answer as it provides some good material.
Brian R. Bondy
Keep this question so that people searching for C++ topics will find it; they may be tempted to ignore the other supposedly C-specific question.
Rob Kennedy
@Rob Kennedy: OK sounds like a good idea. Will not delete.
nbolton
This is definately NOT a duplicate. Under C, free() deallocates the memory allocated by malloc() AND NOTHING ELSE. However, under C++, delete invokes the destructor and then deallocates memory. Since the destructor may perform all sorts of operations, it isn't a good idea to omit calling it.
Stephen C. Steel
+26  A: 

It is important to explicitly call delete because you may have some code in the destructor that you want to execute. Like maybe writing some data to a log file. If you let the OS free your memory for you, your code in your destructor will not be executed.

Most operating systems will deallocate the memory when your program ends. But it is good practice to deallocate it yourself and like I said above the OS won't call your destructor.

As for calling delete in general, yes you always want to call delete, or else you will have a memory leak in your program, which will lead to new allocations failing.

Brian R. Bondy
OK thanks, I will delete religiously from now on. Not just for good memory management, but for better application integrity also.
nbolton
@Nick Bolton: thanks :)
Brian R. Bondy
Don't forget that a class member may also hold something that doesn't automatically disappear with the end of the program, such as a socket or connection or something. Memory isn't the only thing that leaks, and not all leaks can be automatically cleaned up.
David Thornley
@David Thornley: great point.
Brian R. Bondy
+3  A: 

Think about your class 'A' having to deconstruct. If you don't call 'delete' on 'a', that destructor won't get called. Usually, that won't really matter if the process ends anyway. But what if the destructor has to release e.g. objects in a database? Flush a cache to a logfile? Write a memory cache back to disk?

You see, it's not just 'good practice' to delete objects, in some situations it is required.

Stefan
Excellent point, I didn't even think about that.
nbolton
minor correction: "deconstruct" -> "destruct" ;)
phresnel
+4  A: 

Consider the case where the object to be deleted acquired an external resource which can't be freed safely by the OS. If you don't call delete on that object, you have a real leak.

phresnel
+1  A: 

always make sure you delete it yourself. An OS will take care of this but to exclude bugs that can easily be avoided

PoweRoy
+10  A: 

Yes, it helps to eliminate false positives when you run your program through a memory leak detection tool.

Ferruccio
Please could you point me in the direction of some good leak detection tools?
nbolton
On Windows, Compuware's BoundsChecker is excellent, but expensive. On Linux, I've heard good things about Valgrind, but I haven't tried it yet.
Ferruccio
A free one for MSVC++ is Visual Leak Detector (run an internet search - you'll easily find it)
Nemanja Trifunovic
valgrind is wonderful!
Tom
+2  A: 

Another reason for explicitly deleting objects is that if your application has a real memory leak, it becomes easier to use tools like valgrind to find the leaks if you don't have to sift through "leaks" that come from not bothering to clean up.

Mr Fooz
+2  A: 

Another reason to delete is to avoid false alarms from a leak detector you may use in future. If you have false alarms you may not pay attention to a real leak reported by a leak detector - it will be burried among the false ones in the report.

[EDIT] There is an almost identical answer that appeared while I was writing this :)[/EDIT]

Nemanja Trifunovic