BorisSchaeling / boost-process

Boost.Process is a library to manage system processes
Boost Software License 1.0
32 stars 20 forks source link

Potential bug in async wait process example #3

Open angelcarro opened 9 years ago

angelcarro commented 9 years ago

In boost-process/libs/process/example/wait.cpp:52 , the class boost::asio::windows::object_handle is used with the child process handle. I think CloseHandle() will be called twice with the same process handle in the destructors of boost::process::child and boost::asio::windows::object_handle.

DuplicateHandle() can be used to avoid the problem:

#if defined(BOOST_WINDOWS_API)
    DWORD exit_code;
    HANDLE dupHandle;
    DuplicateHandle(GetCurrentProcess(),c.process_handle(),GetCurrentProcess(),&dupHandle,
                    0,false,DUPLICATE_SAME_ACCESS));
    boost::asio::windows::object_handle handle(io_service, dupHandle);
    handle.async_wait(
        [&handle, &exit_code](const boost::system::error_code&)
            { ::GetExitCodeProcess(handle.native(), &exit_code); }
    );
#endif