Aneesh Narayanan
Aneesh Narayanan

Reputation: 3434

Close Events after using it in WaitForMultipleObjects function

I have a vector of event objects, which is used in WaitForMultipleObjects function. After an event is signalled I tried to close the event using the close handle function, but I am getting an error like Invalid handle was specified. Can anybody tell that what the issue is?

std::vector<HANDLE> eventVector;
//..
// Entering data to vector

size_t count = eventVector.size();
DWORD signaledEvent;
While(count > 0)
{
  if (WAIT_OBJECT_0 == (signaledEvent =     WaitForMultipleObjects(handleVector.size(),handleVector.data(), false, INFINITE)))
  CloseHandle(handleVector[signaledEvent - WAIT_OBJECT_0]); // Here I am getting error.
  count--;
}

Upvotes: 0

Views: 525

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

On the first iteration it possibly worked out well, but then once you close the handle, you cannot supply the same vector to WaitForMultipleObjects once again: at least one of the handles is no longer valid.

So WaitForMultipleObjects returns you an error there, and possibly another one later in CloseHandle.

This is not your real code, right? Because in this code snippet you don't really check returned values for errors. Because this code snippet has more errors to note:

  • if(...); - empty conditional block
  • because of the one above you might be accessing vector with illegal index
  • you check WAIT_OBJECT_0 + 0 only, and not other WAIT_OBJECT_0 + N

Upvotes: 1

Related Questions