Handmade Network»Forums
Laurie
37 posts
What resources do you actually need to release?
Hi all. So something I've become curious about is which resources actually need to be released by your program before it exits? Obviously having the ability to release resources during your program's lifetime is crucial to prevent leaks, but what I've always been a bit unsure of is what kind of leaks can actually happen when your program exits?

So for example, managing the memory allocations and deallocations of your program during its lifetime is crucially important, but as I understanding it, any modern OS will clean up all the memory allocated to your program when it exits, so once your program has decided to close, it doesn't need to go through and free all those memory allocations one by one first, because the OS will handle it anyway, right? Assuming that's correct, my question is what, if any, resources actually do need to be released? Can the OS be relied upon to clean up file handles? Sockets? GPU resources? Any insights anyone has on how OSs go about reclaiming resources and where there are potential gotchas here would be really interesting for me. Thanks!
Mārtiņš Možeiko
2559 posts / 2 projects
What resources do you actually need to release?
On any modern OS from last 15 or so years you don't need to release anything on exit of program. OS will release everything for you. That includes memory, file handles, process handles, threads, mutexes, sockets, GPU handles/resources (OpenGL/D3D context/textuer/buffer/etc) and similar OS primitives.
511 posts
What resources do you actually need to release?
But there are things that do need to be released.

I've run into the issue with a printer where I killed a process that was in the middle of printing a page and the spooling process didn't realize the printing process crashes. I had to kill that explicitly.
Mārtiņš Možeiko
2559 posts / 2 projects
What resources do you actually need to release?
Well spooling process is not an OS primitive.
If process A depends on something that process B needs to do (sending event about status, deleting specific file, creating window for something) and process B doesn't do that, then sure - process A will get stuck or hang, and you need explicitly do that thing in process B. I would call it a bug in process A, and not a resource release issue in OS.
Laurie
37 posts
What resources do you actually need to release?
mmozeiko
On any modern OS from last 15 or so years you don't need to release anything on exit of program.

Cool, that's good to know. I take it this applies to mobile OSs and everything too, right?

ratchetfreak
But there are things that do need to be released.

Yeah I've had experiences where I did need to release things too. I've had problems developing OpenCL for example where if I didn't clean up properly and just closed the program, the graphics driver would crash and have to restart. Following what Mārtiņš Možeiko said though, I guess that's more of a driver bug?
511 posts
What resources do you actually need to release?
John_Uskglass
mmozeiko
On any modern OS from last 15 or so years you don't need to release anything on exit of program.

Cool, that's good to know. I take it this applies to mobile OSs and everything too, right?

ratchetfreak
But there are things that do need to be released.

Yeah I've had experiences where I did need to release things too. I've had problems developing OpenCL for example where if I didn't clean up properly and just closed the program, the graphics driver would crash and have to restart. Following what Mārtiņš Možeiko said though, I guess that's more of a driver bug?


Yeah that's something that should be classed as a driver bug. It's not that hard to monitor other processes that interract with your own for whether it is still active or not.