Click to See Complete Forum and Search --> : thread porblem


anima_sg
March 24th, 2004, 11:42 PM
i want to create a UI thread and then end it When i terminate the uI thread by placing WM_QUIT in the message queue and the calling delete on the CWinthread object i get error. If i don't call delete i am worried about the memory leak. can some one help me out

m_pBlankWin - CWinThread

//To start the thread
if(m_pBlankWin==NULL)
{
m_pBlankWin=AfxBeginThread (RUNTIME_CLASS (CBlankThread),0,0,CREATE_SUSPENDED);
m_pBlankWin->m_bAutoDelete=false;
m_pBlankWin->ResumeThread();
}
}
//to terminate the code
if(m_pBlankWin!=NULL)
{

DWORD dwExitcode;
::GetExitCodeThread(m_pBlankWin->m_hThread,&dwExitcode);
if(dwExitcode==STILL_ACTIVE)
{
::PostThreadMessage(m_pBlankWin->m_nThreadID,WM_QUIT,0,0);
delete m_pBlankWin;
}
m_pBlankWin=NULL;
}

wxuf
March 25th, 2004, 01:43 AM
as far as I know, you don't need to delete the thread object.
because You never call "new" for it.

and it is only a pointer, it will be invalid after the thread is exited.

wxuf

anima_sg
March 25th, 2004, 02:13 AM
but i think if we have set autodelete =false the clean will not be done automatically
msdn says
" Objects of class CWinThread typically exist for the duration of the thread. If you wish to modify this behavior, set m_bAutoDelete to FALSE. "

Andreas Masur
March 25th, 2004, 05:41 AM
[Moved thread]

Andreas Masur
March 25th, 2004, 05:43 AM
The automatic deletion of the thread object is controled by the 'm_bAutoDelete' flag of the thread class ('CWinThread').

anima_sg
March 26th, 2004, 12:29 AM
so once i have set m_bautodelete=false i need to delte the the CWinThread object.......that is where the problem is when Post WM_QUIT message and then delete the CWinThread Object i am getting error

Andreas Masur
March 26th, 2004, 08:17 AM
I am not sure whether I completely understand what you are trying to do here. This flag determines whether the automatically created 'CWinThread' class will be destroyed automatically or not. This class is returned by a call to 'AfxBeginThread()'. In other words, if the flag ios set to 'FALSE', you can use the returned pointer to access the members even after the thread has been terminated, if it is set to 'TRUE' you cannot...