ulak blade
ulak blade

Reputation: 2655

Can I close a process's own handle if it's been spawned from another process on Windows?

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

Answers (3)

David Heffernan
David Heffernan

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:

  1. You need to keep the process and thread handles open for as long as you need to use them. So, if you need to wait on either process or thread, you need a handle to the object. Once you no longer need to refer the object you can close the handles.
  2. If you don't close a handle, it will be closed when the process which created the handle terminates. For the example under consideration, that means when the parent process terminates. The system cleans up any resources owned by a process on termination. Sometimes that is a reasonable approach. Typically that makes sense if you need to refer to the object right up until the point where your process terminates.
  3. If you creates process repeatedly, and don't close handles then this can be a resource leak. Obviously you don't want to leak resources.

Upvotes: 1

Werner Henze
Werner Henze

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

Curve25519
Curve25519

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

Related Questions