Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling CloseHandle required?

I was reading http://support.microsoft.com/kb/243953 where they explain how to limit an application to a single instance. A mutex is created upon construction and on destruction they call CloseHandle.

This got me thinking, is calling CloseHandle strictly required even if the application will shut down? In a lot of freebsd c programs the practise of not freeing malloced memory is frequently used because the OS will clean this up faster upon destruction. Could not calling CloseHandle tie up resources in the OS even if the process has shut down?

like image 532
jontro Avatar asked Dec 20 '22 18:12

jontro


1 Answers

Could not calling CloseHandle tie up resources in the OS even if the process has shut down?

No. When a process is terminated the OS implicitly releases any handles it had been keeping around, exactly like it does with allocated memory.

From the viewpoint of the system, the drawback of not closing handles is again the same as the drawback of not freeing memory: you prevent the OS from freeing up logically unused resources as long as the process is still running.

From the viewpoint of the developer, the drawback is that there is no way to automatically partition allocated resources into sets of "leaked on purpose" and "leaked due to a bug". When (probably not if) at some point you realize that your app is leaking resources it really shouldn't be, open handles like these are going to make your life more difficult by helping the real problem hide among false leads.

like image 158
Jon Avatar answered Dec 22 '22 07:12

Jon