c++ - CloseHandle confusion - Must I call CloseHandle on multiple "copies" of a handle? -
i have (more) questions calling closehandle.
so, the citizens have spoken, , must close handle.
question 1
i've written following code snippet in destructor:
handle handles[] = { m_hgrabberthread, m_hctrlthread, m_herrdispatchthread }; int nnumhandles = sizeof(handles) / sizeof(handles[0]); for( int n = 0; n < nnumhandles; n ++ ) closehandle( handles[n] ); is above code valid, or must call closehandle() on each handle member variable individually?
e.g.
if( m_hctrlthread != invalid_handle_value ) closehandle( m_hctrlthread ); i suppose question linked (vaguely) question 2...
question 2
i have class creates event handle:
handle heventabortprogram = createevent( null, true, false, null ); this handle shared among other threads in other objects.
by sharing handle, mean:
objectb.m_heventabort = objecta.m_heventabort; each object's threads like:
while( waitforsingleobject(m_heventabort, 0) == wait_timeout ) {...} when event signaled, threads exit.
my question is: must call closehandle on each copy of handle, or once in main "parent" object?
i suppose i'm asking - handles reference counted when they're copied?
i know handle typedef void*, instinct says no, need call once per handle.
to question 2: number of calls closehandle should balance number of calls handle creation functions. if assign handle handle variable, have not created new handle - 2 handles have same value. can share handle value as want, 1 object must close handle.
if can't guarantee destruction order of objects sharing handle; can use duplicatehandle make additional handles existing handle. each additional handle created need closed, , underlying object handles reference released when handles closed.
Comments
Post a Comment