Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WaitForSingleObject Crashing

591 views
Skip to first unread message

Sean Branney

unread,
Jul 30, 2004, 1:03:58 PM7/30/04
to
Hi,
I've recently gotten into multi threaded programming as part of a
application that streams audio, with a thread that is used to keep the
Direct Sound buffer filled with data (pretty much straight out of the DX
SDK). I'm having a problem with the cleanup of the thread though. The
following line of code will very occasionally bring the application to a
crashing halt:

returnVar = WaitForSingleObject(pSSS->hNotifyThread, INFINITE);

Assuming this is sucessfull I would then close the handle.

I can't check the value of returnVar at this point as the application just
crashes straight out. Almost everytime though returnVar == WAIT_OBJECT_0
which I understand to mean the object has been secured sucessfully.

The error code returned each time is 0xc0000005, which through searching
appears to correspond with a memory out of bounds error? Or trying to
access something which isnt accessable?

I've tried encapsulating the function call in a try{ }catch(...){} exception
handling routine with no sucess.

Any help at all would be much appreciated,

Sean Branney

p.s. I've experimented with removing the WaitForSingleObject call altogther,
but all that suceeded in doing was having the program crash at a random
location


Sean Branney

unread,
Jul 30, 2004, 1:19:03 PM7/30/04
to
Update:

Having read the 'Is SendMessage from worker thread really evil?' posts
(apologies for posting without reading all first :$) I'll include a little
piece of code that occurs before the two previously posted:

PostThreadMessage(pSSS->dwNotifyThreadID, WM_QUIT, 0, 0);

Would this have anything to do with not being able to get a hold of the
thread handle?


Norman Bullen

unread,
Jul 30, 2004, 8:31:56 PM7/30/04
to

Just for clarification, are you saying that PostThreadMessage() is
called immediately before WaitForSingleObject()?

Is it possible that the thread handle (pSSS->hNotifyThread) is closed
before the WaitForSingleObject() can return?

Are you sure that the error actually occurs _in_ WaitForSingleObject()
or is it possible that pSSS has become corrupted leading to an exception
when trying to get the value of pSSS->hNotifyThread prior to entering
WFSO()?

Does the thread do anything significant after receiving the WM_QUIT message?

Norm

--
--
To reply, change domain to an adult feline.

Raymond Chen

unread,
Jul 31, 2004, 5:03:39 AM7/31/04
to
On Fri, 30 Jul 2004 17:03:58 GMT, "Sean Branney"
<scot...@hotmail.com> wrote:
>returnVar = WaitForSingleObject(pSSS->hNotifyThread, INFINITE);
>
>I can't check the value of returnVar at this point as the application just
>crashes straight out. Almost everytime though returnVar == WAIT_OBJECT_0
>which I understand to mean the object has been secured sucessfully.
>
>The error code returned each time is 0xc0000005, which through searching
>appears to correspond with a memory out of bounds error? Or trying to
>access something which isnt accessable?

0xc0000005 = STATUS_ACCESS_VIOLATION. I suspect somebody freed
pSSS and so your attempt to access it crashes.

Sean Branney

unread,
Jul 31, 2004, 7:55:03 AM7/31/04
to
Norm, thank you for your reply. I have provided as much information as I
think is relevant, maybe a little more. Hoping this will clarify my
dilemma.

> Are you sure that the error actually occurs _in_ WaitForSingleObject()
> or is it possible that pSSS has become corrupted leading to an exception
> when trying to get the value of pSSS->hNotifyThread prior to entering
> WFSO()?

I have logged the events right up until the function call to
WaitForSingleObject (or variant). Right before calling I log this:

sprintf(output, "Wait for Notification Thread - %d", pSSS->hNotifyThread);
eventLog.writeToLog(output);

and I'm not getting any funny numbers there.

e.g. Wait for Notification Thread - 1436

> Just for clarification, are you saying that PostThreadMessage() is
> called immediately before WaitForSingleObject()?

*Short Answer*: Yes.

The code of the function minus the eventlogging is:

void freeStreamingSound(StreamingSoundStruct* pSSS)
{
int returnVar;

SAFE_DELETE( pSSS->pStreamingSound);

PostThreadMessage(pSSS->dwNotifyThreadID, WM_QUIT, 0, 0);

//returnVar = WaitForSingleObjectEx(pSSS->hNotifyThread, INFINITE, true);
//returnVar = WaitForSingleObject(pSSS->hNotifyThread, INFINITE);
returnVar = MsgWaitForMultipleObjects( 1, &pSSS->hNotifyThread, true,
INFINITE, QS_ALLEVENTS );

returnVar = CloseHandle(pSSS->hNotifyThread);

returnVar = CloseHandle(pSSS->hNotificationEvent);
}

I've included the variant calls I've tried to each with the same results.

> Is it possible that the thread handle (pSSS->hNotifyThread) is closed
> before the WaitForSingleObject() can return?

I wouldn't know if this was possible or not, as I'm not entirely sure what
would close the handle. If the thread coming to an end closes the handle,
then that might cause the problem. In which case a sleep call after
receiving the WM_QUIT message would fix it. But I've just tried that and
again no joy.

There aren't any erroneous calls to CloseHandle that could be closing
pSSS->hNotifyThread.

pSSS is a pointer to struct containing

CStreamingSound* pStreamingSound;
HANDLE hNotificationEvent;
DWORD dwNotifyThreadID;
HANDLE hNotifyThread;
bool looping;

This struct is a member of HubIntroGlobals which is freed in the function
which calls freeStreamingSound:

void freeHubIntro()
{
p_HubIntroGlobals->movie.CloseAVI();

freeStreamingSound(&p_HubIntroGlobals->soundStream);

SAFE_DELETE(p_HubIntroGlobals);
}

> Does the thread do anything significant after receiving the WM_QUIT
message?

* Short Answer*: No.

The main code of the threaded function is:

while( !bDone )
{
dwResult = MsgWaitForMultipleObjects( 1, &hNotificationEvent,
FALSE, INFINITE,
QS_ALLEVENTS );
switch( dwResult )
{
case WAIT_OBJECT_0 + 0:
// g_hNotificationEvent is signaled
// This means that DirectSound just finished playing
// a piece of the buffer, so we need to fill the circular
// buffer with new sound from the wav file
if( FAILED( hr = pSS->HandleWaveStreamNotification(
looping ) ) )
{
DXTRACE_ERR_MSGBOX(
TEXT("HandleWaveStreamNotification"), hr );
MessageBox(NULL,"Error handling DirectSound
notifications.","ERROR",MB_OK|MB_ICONEXCLAMATION);
bDone = TRUE;
}
break;

case WAIT_OBJECT_0 + 1:
// Messages are available
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
bDone = TRUE;
}
break;
}

return 0;

As soon as the WM_QUIT message is received the loop is exited.


Sean Branney

unread,
Jul 31, 2004, 8:00:55 AM7/31/04
to
Raymond, thank you for your reply.

> 0xc0000005 = STATUS_ACCESS_VIOLATION. I suspect somebody freed
> pSSS and so your attempt to access it crashes.

pSSS is freed immediately after the function is called. Also the members of
pSSS are accessed as late as the line before the function call to
WaitForSingleOjbect, with no funny numbers involved, so I don't think that
this is the case. The logging code is:

sprintf(output, "Wait for Notification Thread - %d", pSSS->hNotifyThread);
eventLog.writeToLog(output);

A typical response right before a crash is "Wait for Notification Thread -
1436" which doesn't differ from the norm at all.

Sean


Scott McPhillips [MVP]

unread,
Jul 31, 2004, 10:28:45 AM7/31/04
to
Sean Branney wrote:
>
> The code of the function minus the eventlogging is:
>
> void freeStreamingSound(StreamingSoundStruct* pSSS)
> {
> int returnVar;
>
> SAFE_DELETE( pSSS->pStreamingSound);
>
> PostThreadMessage(pSSS->dwNotifyThreadID, WM_QUIT, 0, 0);
>
> //returnVar = WaitForSingleObjectEx(pSSS->hNotifyThread, INFINITE, true);
> //returnVar = WaitForSingleObject(pSSS->hNotifyThread, INFINITE);
> returnVar = MsgWaitForMultipleObjects( 1, &pSSS->hNotifyThread, true,
> INFINITE, QS_ALLEVENTS );
>
> returnVar = CloseHandle(pSSS->hNotifyThread);
>
> returnVar = CloseHandle(pSSS->hNotificationEvent);
> }

It looks worrisome that you are using MsgWaitForMultipleObjects. If a
message comes in the function returns before the thread exits.

--
Scott McPhillips [VC++ MVP]

Sean Branney

unread,
Jul 31, 2004, 10:06:25 AM7/31/04
to
I think I've identified the problem as being the thread attempting to fill
the buffer with sound after I've freed the buffer (I was freeing the buffer
before closing the thread... doh).

Thanks everybody for your help.

Sean


0 new messages