Anonymous pipes

I wrote two short programs that use anonymous channels for communication. The parent process shares the channel descriptors by setting the standard I / O descriptors for the child:

// -- Set STARTUPINFO for the spawned process -------------------------
ZeroMemory(&m_ChildSI, sizeof(STARTUPINFO));
GetStartupInfo(&m_ChildSI);

m_ChildSI.dwFlags       = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
m_ChildSI.wShowWindow   = SW_HIDE;
m_ChildSI.hStdError     = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdOutput    = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdInput     = m_pipeParent.ReadPipeHandle();

A child acquires a read read handle with a GetStdHandle call :

hReadPipe = GetStdHandle(STD_INPUT_HANDLE)

My question is: Pipe handles are created by a parent process that calls CloseHandle () on them as soon as the parent and child have completed the conversation.

Does the child also need to call CloseHandle ()? I thought that since these are standard I / O descriptors, they are automatically freed up when the process changes.

thank!

+3
source share
3 answers

Win32 , , . , .

, , .

+2

MSDN, :

" , , CloseHandle , ."

+1

Any descriptor may remain open when the application terminates, Windows will automatically free resources. But it’s best to close them manually so that everything is logical and consistent. Opening handles can lead to errors and leaks when reusing or upgrading code.

0
source

Source: https://habr.com/ru/post/1760533/


All Articles