Note that there are some explanatory texts on larger screens.

plurals
  1. POCloseHandle confusion - Must I call CloseHandle on multiple "copies" of a handle?
    primarykey
    data
    text
    <p>I have some (more) questions about calling CloseHandle.</p> <p>So, <a href="https://stackoverflow.com/questions/3959473/must-i-closehandle-on-a-thread-handle">the SO citizens have spoken, and you must always close a handle</a>. </p> <p><strong>Question 1</strong></p> <p>I've written the following code snippet in a destructor:</p> <pre><code>HANDLE handles[] = { m_hGrabberThread, m_hCtrlThread, m_hErrDispatchThread }; int nNumHandles = sizeof(handles) / sizeof(handles[0]); for( int n = 0; n &lt; nNumHandles; n ++ ) CloseHandle( handles[n] ); </code></pre> <p>Is the above code valid, or must I call CloseHandle() on each handle member variable individually?</p> <p>e.g.</p> <pre><code>if( m_hCtrlThread != INVALID_HANDLE_VALUE ) CloseHandle( m_hCtrlThread ); </code></pre> <p>I suppose that this question is linked (vaguely) to question 2...</p> <p><strong>Question 2</strong></p> <p>I have a class that creates an event handle:</p> <pre><code>HANDLE hEventAbortProgram = CreateEvent( NULL, TRUE, FALSE, NULL ); </code></pre> <p>This handle is shared among other threads in other objects.</p> <p>By sharing the handle, I mean:</p> <pre><code>objectB.m_hEventAbort = objectA.m_hEventAbort; </code></pre> <p>Each object's threads will then do something like:</p> <pre><code>while( WaitForSingleObject(m_hEventAbort, 0) == WAIT_TIMEOUT ) {...} </code></pre> <p>When the event is signaled, all threads will exit.</p> <p>My question is: must I call CloseHandle on each copy of the handle, or just once in my main "parent" object? </p> <p>I suppose that I'm asking - are handles reference counted when they're copied? </p> <p>I know that a handle is only a typedef for a void*, so my instinct says no, I only need to call it once per handle.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload