Is releasing objects on a programs exit/close needed?
In other words, let us say for the sake of argument, you have a button that closes your application, but right before you close you display an image, and then you close the application.
Do you need to release that image view before you close the application? Will the memory automatically be freed when the program exits, or if you don’t release it will the memory stay somehow ‘active’?
I understand that you ‘should’ release it, my question is about the technical side of it, and what happens behind the scenes.
It’s not necessary. But if you’re using
valgrindor a similar tool, you’ll soon discover that leaving all of your memory dangling swamps you with false warnings.On the Linux side of things, the heap is grown using the
sbrksystem call. This grows the overall processor memory space by a large block at a time (so only onesbrkis needed to provide enough space for manymallocs). When the process goes away, the kernel reclaims all of the memory allocated bysbrk. This is why you’re safe. The kernel will also close any file descriptors opened by that one process.There are a few problems that can come up. If your process ever
forks at an inopportune moment, any open file descriptors will be duplicated. I have seen this manifest itself as a TCP connection mysteriously hanging alive after the original process died, which is nasty. In addition, there are other resources that are just not process-scoped, so they won’t be reclaimed when the process dies. This includes shared memory segments, temporary files, named pipes and UNIX sockets, and probably a slew of other IPC mechanisms.In summary? Memory is fine. File descriptors are usually fine. Some of the more esoteric IPC features will be horribly broken if not cleaned up.