Reputation: 2655
I have a process that calls the function CreateProcess()
, however I never call CloseHandle()
from the parent process, because I don't know when the user would exit the child process. Does this result in some kind of leak or is it ok, since when the child process closes, it frees up all its resources?
Upvotes: 0
Views: 1530
Reputation: 612794
A couple of quotes from the question and comments:
I never call
CloseHandle()
from the parent process, because I don't know when the user would exit the child process.
And:
The thread that spawns the process continues to do other things afterwards and is expected to continue long after the child process closes. So I can't close it just yet?
This I think is your fundamental misunderstanding. A handle is simply you means of interacting with the process or thread objects. Closing the handle does not terminate the object. It simply tidies up the resources around the handle. The object, process or thread, continues to live after your handle to it has been closed.
If you need to terminate the process, call TerminateProcess
passing your process handle. At that point, you are generally safe to close the handle.
Here are the rules that you need to follow:
Upvotes: 1
Reputation: 16726
If you do not close the handle, you have a handle leak.
You should keep the handle as long as you need it to pass it to another WinAPI function, for example if you need information about the process like a) did it terminate, b) what is the return value, c) how much processor time does it consume. You can close the handle when you do not need it anymore. Closing the handle will not close the child process, that will still continue running!
You can find this and more information about the impacts on MSDN for PROCESS_INFORMATION structure in the section Remarks. Please also note that you need to CloseHandle
the thread handle.
Upvotes: 6
Reputation: 694
You can close the handle to the process. Unless you call TerminateProcess
, or it is closed by other method the process will continue to exists.
If you don't need to handle, call the CloseHandle
, and it's preferred that you'll do that as soon as possible.
Still owning a handle to the process will cause a resource leak. Though the process and its resources will be deleted when the process terminates, some of it's info will still continue to exists unless you close that handle .
Upvotes: 0