In any modern operating system the virtual memory pages feature. In this model each process owns an entire address space and the system has the responsibility to map the address that the process wants to read to the address that memory really is at. This gives the system several useful optimization opportunities, such as placing unused pages on disk (swap), rescuing them when required, or applying copy-on-write (Cow) where two processes share the same page.
This way the system already knows exactly which memory belongs to each process. And releasing that memory is as simple as marking it as a junk page that can be reused in another process. That is, for any modern computer, finishing a process means releasing all your memory.
Considering a large program, there are several problems when you press "close". The destructor of all objects will be executed in sequence, causing a half-dead memory pile to be accessed and read from the disk (in case of a swap) and causing visible slowness to the end user. You’re wasting the lifetime of your hard drive while wasting user time, all wrong. This just to free resources that will already be released anyway by the system, and more efficiently (frees pages instead of releasing object by object, and does not need to read data from disk).
If this is good practice, it is questionable. Object orientation design expects that everything that is built will at some point be destroyed. Then there may happen to be some important logic going on in the destructor, like saving settings on disk. In this case merely calling exit(0)
can be harmful. Another way would be to have a Boolean global variable that is true when the process is about to close and have each destructor check this variable, skipping unnecessary memory releases. But here there is little advantage because a lot of memory will need to be read anyway (we return to swap) and the logic of destruction becomes complicated and non-trivial.
A solution may be to have a notification mechanism that sends some kind of signal to all objects that need to do something more at program termination. It could be a list of callbacks for example. This is probably the most efficient way not to completely break the object-oriented structure.
The big problem is that tools that detect Leak will accuse a lot of problems in your program. Hence it is interesting to have two terminating paths, one dislocating everything (for debug) and the other going straight to the exit(0)
. Both should be equally tested, of course. But the second need not go through the Leak detector.
But remember that there are resources other than memory. The inter-procedural communication mechanisms of System V (available in some *Nix, including linux), for example, are permanent and survive the termination of the by-design process. For them it is essential to release.
All this discussion is only valid for languages where you programmer does the memory handling. For languages with garbage collector usually there won’t be much choice.
If the Dollynho Programmer says that you can leave it allocated yes when the program is terminated, then it is because you HAVE to de-locate when the program terminates XD
– Oralista de Sistemas