Reputation: 3032
I use Windows API CreateProcess
function. In the msdn manual for it, I have found
If the function succeeds, be sure to call the CloseHandle function to close the hProcess and hThread handles when you are finished with them. Otherwise, when the child process exits, the system cannot clean up the process structures for the child process because the parent process still has open handles to the child process.
So where I should do this?
The situation is that the process is started and there is no track of his life. Does it force me to make a thread, where the process will be created and then the thread will wait using, for example WaitForSingleObject
, until the process will be dead, so the handles could be released?
Upvotes: 1
Views: 1496
Reputation: 6657
You don't have to wait for the child process to finish, you just have to CloseHandle
when you no longer need the handle.
The reason for this is that you may want to keep a handle to the process even after the process has finished. You may want to check its return status, for example. As long as you have a handle to it, Windows can't clean up after it.
But if you don't care anymore what the child does, close the handle and move on.
If you do care, call WaitForSingleObject
and give it the handle you got from CreateProcess
. Or call RegisterWaitForSingleObject
(again passing it the process handle) with a callback function that will be called when the process ends.
Upvotes: 4