Re: returning from worker thread
I realize my posts in this thread are possibly becoming an annoyance as I
juggle and struggle among varying issues and code attempts. But this
particular question is not related to shutdown, which actually was provided
for before experimenting with the advice I have received. May I ask this --
Assuming that my audio class waveOut calls m_pAudioThread->ResumeThread(),
is there anything in my worker thread function below that would appear
problematic specifically in relation to being able to post a message to my
view and retain valid pointers?
UINT AudioThreadFunc(LPVOID pParam)
{
while(TRUE)
{
CAudioFile* audiofile = (CAudioFile*)pParam;
if(WaitForSingleObject(audiofile->m_hndDone,INFINITE) != WAIT_OBJECT_0)
// error handling goes here
return 0;
}
CloseHandle(audiofile->m_hndDone);
if(audiofile->m_pView->m_hWnd)
audiofile->m_pView->PostMessage(WM_AUDIO_CLEANUP, 1); // handler
calls m_pAudioFile->AudioCleanUp()
else
audiofile->AudioCleanUp(false);
::SuspendThread(audiofile->m_pAudioThread->m_hThread);
}
return 0;
}
---------------
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:eByn7ljvGHA.4688@TK2MSFTNGP06.phx.gbl...
Steve Russell wrote:
Even with keeping the thread alive, I find, in Release, that using
audiofile->m_pView->PostMessage(WM_AUDIO_CLEANUP)
instead of calling audiofile->CleanUp() directly in the thread
leads to trouble with the view pointer eventually. Aside from needing to
change my threading design, I would sure appreciate an explanation as to
how this could be happening. If I the thread isn't being destroyed, how
is it that the audio class pointer passed to my thread function would
fail?
Your code outline has not shown any provision for thread shutdown at
program close. The likely problem with the view pointer is that the view
object gets destroyed before the thread has exited. This opens the door
for the thread to use an invalid pointer.
You must control the program shutdown sequence so the thread is gone
before any shared data, such as a view pointer, becomes invalid. Several
posts in this thread, as well as the Newcomer tutorials, point this out
and mention ways to do it. Data known to the thread must remain valid
until after the thread has exited!
--
Scott McPhillips [VC++ MVP]