Microsoft played safe here. In their article, "Creating a Child Process with Redirected Input and Output", they are saying:
The remaining open handles are cleaned up when this process terminates.
To avoid resource leaks in a larger application, close handles explicitly.
Which is perfectly useless. What handles? In which process?
I want to get my head around it.
When a handle is created in parent process with SECURITY_ATTRIBUTES.bInheritHandle = TRUE
, a child process can see and use it, and the handle has the same value and access rights in both processes.
But is it the same handle, or is it a copy that happen to have same numerical representation?
If I pass a hRead
handle to a child process so that it can read from a pipe, and the child process closes the handle, do I also need to close it from the parent process? Will it not wipe the pipe from under the child process?
My experiments show that CloseHandle
returns success when trying to close a hRead
handle passed to the child after the child has closed it. This argues strongly for Yes, you should close it. However, I would appreciate a more solid advice here.