Handmade Hero»Forums»Code
Robert Toth
12 posts
Free memory before application exit?
Hi!

I remember Casey explaining how it's not necessary to free memory before exiting an application because the operating system does that for us. Did I understand that correctly? Is this always true?

I'm taking a short cut to be able to get started on my application so I'm using SDL, but every tutorial I've ever seen (even the wiki) says that I need to call the following just before returning from the main function:
1
2
3
4
5
// Destroy window
SDL_DestroyWindow(window);

//Quit SDL subsystems
SDL_Quit();

But that should not be needed then?

/Robert from Sweden
Mārtiņš Možeiko
2242 posts / 1 project
Free memory before application exit?
Edited by Mārtiņš Možeiko on
Yes, you understood correctly. When OS process terminates the OS deallocates any resource allocated - for example, memory, file handles, created windows, threads, mutexes and everything else. This is generally true for modern OS'es like Windows, OSX, Linux.

But sometimes you need to control resources in much shorter time span than whole process lifetime. Or you maybe are writing library that users will use and you don't know when they will need to deallocate resources. In such cases you need to deallocate resource manually. You will see this happening on stream when Casey implemented debug file API and also OpenGL texture handling for bitmap assets.
498 posts
Free memory before application exit?
In general the OS will clean up after you after the program shuts down. However it's possible that there are resources that don't get cleaned up, usually when doing communication with another (non-kernel) process and that one doesn't realize the other party crashed or shut down.

Personally I've had it happen when debugging some printing code. The PDF printer driver didn't realize that I had killed my process in the middle of printing and was stuck in "spooling" until I had to kill that process as well.
Mārtiņš Možeiko
2242 posts / 1 project
Free memory before application exit?
Terminating process or process simply crashing (because or bug or whatever) is completely different story. In such case usually no cleanup code runs at all. So there is no way to signal other process that it shouldn't wait on my. For example, if code is structured like this:
1
2
3
4
5
while (running)
{
   ...
}
SendMessageToOtherProcessSoItKnowsItShouldCleanUpMyResources();

Then killing it when while loop is executing won't call that SendMessage function. And your printer will still show stuck document in queue.

Similarly if process cleans up some resources in global C++ destructors or C atexit callback - those things won't run when process crashes or you are killing it. As long as regular resources (memory, handles, threads, etc..) are deallocated, everything will be ok.
498 posts
Free memory before application exit?
But it also means that the cleanup should not be skipped for those kind of multiprocess resources and a general "don't cleanup at shutdown" stance is not safe but depends on the resource being cleaned up.
Robert Toth
12 posts
Free memory before application exit?
Thanks for the answers.

I thought that I could skip cleaning up if I knew the application was going to exit because of the virtual memory space that the process gets when starting up. Can't the OS just wipe the part of actual memory space that the virtual one points to when the application has terminated? Then it shouldn't matter if it crashed or exited nicely since the OS should know that the process is dead, right?
Mārtiņš Možeiko
2242 posts / 1 project
Free memory before application exit?
Edited by Mārtiņš Možeiko on
Yes, that's exactly why you can skip cleaning up, because OS will deallocate all virtual memory for process (whether it crashed or exited normally - it doesn't matter for OS).

Wiping memory is not necessary for "resource cleanup" purposes. It is security feature - you don't want another process to get access to your data when it gets memory block that you had before. On Windows wiping memory happens in separate low-priority thread: https://blogs.msdn.microsoft.com/...ory-management-revealed-part-two/
498 posts
Free memory before application exit?
Raybolio
Thanks for the answers.

I thought that I could skip cleaning up if I knew the application was going to exit because of the virtual memory space that the process gets when starting up. Can't the OS just wipe the part of actual memory space that the virtual one points to when the application has terminated? Then it shouldn't matter if it crashed or exited nicely since the OS should know that the process is dead, right?


Don't get me wrong memory will be cleaned up. As will the window and file handles and various other resources.

I was talking about other resources (that aren't necessarily from the OS).
Robert Toth
12 posts
Free memory before application exit?
Ah, I see. Thanks guys!

/Robert