I plan to use the WinApi CreateMutex function to only allow one running instance of my application. But I wonder what happens if the apps crashs. Is the created Mutex automatically released by the OS if the main process dies? I can't find an answer to this in the MS knowledgebase. TIA!
1 Answer
A mutex is a kernel object whose lifetime is controlled by its references. When a process terminates, however it terminates, all the references to kernel objects held by that process are removed. If this leaves a kernel object having no remaining references to it, that kernel object is destroyed.
-
The mutex will be destroyed when there are no more references to it. The issue is more complex when there are multiple processes with references to the mutex. You then need all the processes that have references to clear those references (either with a call to
CloseHandle
or by the process terminating). But suppose only a single process has a reference to the mutex. Then when that process ends, the mutex is destroyed. Jun 30, 2014 at 11:51 -