Should I close the inherited descriptor, which then belongs to the child process?

Microsoft played here safely. In their article “Creating a child process with redirected inputs and outputs,” they say:

The remaining open handles are cleared when this process is completed. Explicitly close handles to avoid resource leaks in a larger application.

It is completely useless. What pens? In which process?


I want to get around him.

When a handle is created in the parent process using SECURITY_ATTRIBUTES.bInheritHandle = TRUE , the child process can see and use it, and the handle has the same value and access rights in both processes.

But is this the same descriptor, or is it a copy that has the same numerical representation?

If I pass the hRead handle hRead child process so that it can read from the pipe and the child process closes the handle, do I also need to close it from the parent process? Would he wipe the handset out of the child process?

My experiments show that CloseHandle returns success when trying to close the hRead descriptor hRead to the descendant after the child closed it. That says a lot about yes, you have to close it. However, I would appreciate more solid advice.

+7
source share
1 answer

You hit your head with a fingernail. Win32 handles act as user mode references for the core kernel mode object. Pens (links) to new and existing objects are usually created using a call to CreateXXX - additional links can be created in the current or other processes by calling DuplicateHandle - {or marking the handle as inherited and creating a new process - which duplicates the handle of the current process in the child process - guarantees the identity of the descriptor value.}

At this point, there are (at least) two references to the kernel object, both of which must be closed in order to unlock the object and free up any consumed resources.

+10
source

All Articles