- MsgWaitForMultipleObjects returns ok, but event was not signaled.
- Posted by Mario Semo on January 21st, 2004
Hello,
I am debugging a problem where it seems that a semaphore is being released
too early.
The API MsgWaitForMultipleObjects() returns with a proper returncode that
indicates that the event waiting for has been set to signalled state.
But this is not currect, the other thread i am waiting for has not set the
event yet.
Are there any known problems with this API that causes Windows2000 SP3 to
cause this effect?
What else can set an event semaphore to signalled state apart from the API
SetEvent() ??
Is there any way to find out how or by which thread the event has been
signalled?
The problem is NOT that the API returns because there are inputs in the
thread´s input queue.
This returncode is handled by the code, the input queue will be read and the
wait operation continued.
The code snippet that does the wait for the event semaphore is:
DWORD rc = WAIT_OBJECT_0 + pNum;
HANDLE* handle = new HANDLE[pNum];
for (INumber lCnt=0;lCnt<pNum;lCnt++)
{
handle[lCnt] = (HANDLE)(pSemArray[lCnt]->hev);
}
while (rc == (WAIT_OBJECT_0 + pNum))
{
rc = MsgWaitForMultipleObjects
(
pNum // number of elements
,handle // pointer to the handle-array
,FALSE // any object is enough
,ulTimeout // time-out
,QS_ALLINPUT
);
if (rc == WAIT_OBJECT_0 + pNum)
{
MSG msg;
while (PeekMessage (&msg,(HWND) NULL,0,0,PM_NOREMOVE))
{
// perform real message processing now!
if (GetMessage(&msg,(HWND)NULL,0,0))
{
DispatchMessage(&msg);
}
}
}
}
delete[] handle;
Any help, suggestions and comments appreciated!
--
mit freundlichen Grüßen/best regards
mario semo
- Posted by Lucian Wischik on January 21st, 2004
"Mario Semo" wrote:
I'm not aware of any known problems that'd cause this. I reckon the
error is somewhere in your program logic. If you could post a complete
self-containing mini-program which demonstrates the problem, I'd be
happy to help!
--
Lucian
- Posted by Mario Semo on January 22nd, 2004
Lucian,
Of course this was the first suspect, but code-reviews and debugging showed
other facts:
Timeout is set to INFINITE (0xFFFFFFF)
There does no timeout occur, i am checking all of the important returncodes:
WAIT_ABANDONED_0, 0xffffffff (failed), WAIT_TIMEOUT.
The returncode i get is WAIT_OBJECT_0, so everything should be ok.
Even GetLastError() returns 0.
The handle i am waiting for has been created with CreateEvent,it is an
unnamed event semaphore and can therefore also not be signalled by another
process!
Is it possible that the event i am waiting for could also be signalled if
someone calls ReleaseSemaphore or another API?
I have set a breakpoint at the beginning the SetEvent() and also the
PulseEvent() API, and checked the stack for the handle i am waiting for.
The SetEvent() API is never called with my handle, but the event is
signalled somewhere anyway!
Unfortunately it is not possible to reduce the scenario to a mini-sample.
mit freundlichen Grüßen/best regards
mario semo
"Lucian Wischik" <lu.nn@wischik.com> wrote in message
news:993t009mvmal21f32v534oenflfqrrqvh3@4ax.com...
- Posted by Mario Semo on January 22nd, 2004
Hello,
one more comment:
We have also tried to replace the MsgWaitForMultipleObjects() with
WaitForSingleObject(), and the effect is the same:
The event gets signalled without a SetEvent() or PulseEvent() call.
--
mit freundlichen Grüßen/best regards
mario semo
"Mario Semo" <mario.semo@kirchnersoft.com> wrote in message
news:buo7u9$sd9$1@at-vie-newsmaster01.nextra.at...
- Posted by Mario Semo on January 22nd, 2004
Finally we found the bug, it was really a bug in our code:
The problem was a API call to LockFileEx() where the hEvent member of the
OVERLAPPED structure was uninitialized.
Due to very bad luck, the handle of the event semaphore we are waiting for
has been on the memory within the stack, and so the LockFileEx() API
signalled the event semaphore. This caused the event being signalled too
early and finally crashed the program (because the event should have been
signalled somewhere later, after finishing a certain operation).
--
mit freundlichen Grüßen/best regards
mario semo
"Mario Semo" <mario.semo@kirchnersoft.com> wrote in message
news:buli06$193$1@at-vie-newsmaster01.nextra.at...