Click to See Complete Forum and Search --> : Memory Leak Help


cb_schear
March 27th, 2004, 04:24 PM
As far as I can tell this thread is stoping when I press the cancel button, but for some reason I keep getting this error:

Detected memory leaks!
Dumping objects ->
thrdcore.cpp(311) : {82} client block at 0x00326A10, subtype c0, 68 bytes long.
a CWinThread object at $00326A10, 68 bytes long
{81} normal block at 0x003269C8, 12 bytes long.
Data: < i2 l > 90 0F 00 00 80 69 32 00 C0 04 6C 00
Object dump complete.


I cant seem to figure out were this memory leak is coming form, hopefuly you guys can see it.

1st I Create A Dialog With DoModal()

CProgressDlg::CProgressDlg(CWnd* pParent /*=NULL*/)
: CDialog(CProgressDlg::IDD, pParent)
{
m_pWorkThread = NULL;
m_pMFileInfo = NULL;

m_ThreadStop = CreateEvent(0, TRUE, FALSE, 0);
}

BOOL CProgressDlg::OnInitDialog()
{
CDialog::OnInitDialog();

StartWorkerThread();

return TRUE;
}

void CProgressDlg::StartWorkerThread(void)
{
m_pMFileInfo = new CMFileInfo;
CMFileInfo::ThreadItem* pThreadItm = new CMFileInfo::ThreadItem;
pThreadItm->dHWND = GetSafeHwnd();
pThreadItm->pThis = m_pMFileInfo;
pThreadItm->hStopEvent = m_ThreadStop;

m_pWorkThread = AfxBeginThread(m_pMFileInfo->CreateList, pThreadItm,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);

if (m_pWorkThread == NULL)
{
MessageBox("Error In Sub Thread Creation.", "Error", MB_OK | MB_ICONERROR);
delete pThreadItm;
EndDialog(IDCANCEL);
}
else
{
m_pWorkThread->m_bAutoDelete = false;
m_pWorkThread->ResumeThread();
}

}

void CProgressDlg::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here

StopWorkerThread();


OnCancel();
}

void CProgressDlg::StopWorkerThread()
{
SetEvent(m_ThreadStop);

WaitForSingleObject(m_pWorkThread, INFINITE);
}

CProgressDlg::~CProgressDlg()
{
if (m_pMFileInfo != NULL)
{
delete m_pMFileInfo;
m_pMFileInfo = NULL;
}

CloseHandle(m_ThreadStop);

}


The class CMFile has a static function that (as of right now) just goes through a loop until I press the cancel button on the above dialog.

UINT CMFileInfo::CreateList(LPVOID pParm1)
{
ThreadItem* pTItem = static_cast<ThreadItem*>(pParm1);

pTItem->pThis->m_DlgHwnd = pTItem->dHWND;
pTItem->pThis->m_StopEvent = pTItem->hStopEvent;

pTItem->pThis->BuildMList();


delete pTItem;
pTItem=NULL;

AfxEndThread(0, true);

return 1;
}

void CMFileInfo::BuildMp3List(void)
{

while(!UserCanceled() )
{
Sleep(200);
}

}

bool CMFileInfo::UserCanceled()
{
if (WaitForSingleObject(m_StopEvent, 0) == WAIT_OBJECT_0)
return true;
else
return false;
}

cb_schear
March 27th, 2004, 04:45 PM
Ok, I traked down part of the problem. Its here:

WaitForSingleObject(m_pWorkThread, INFINITE);


For some reason it returns "ERROR_INVALID_HANDLE". ??

cb_schear
March 27th, 2004, 04:53 PM
Problem Solved.

Just incase anyone else needs to know you have call it like so:
WaitForSingleObject(m_pWorkThread->m_hThread, INFINITE);

Also you need to delete the pointer to the thread (if you set it to auto delete false)

Andreas Masur
March 28th, 2004, 06:13 AM
[Moved thread]

Andreas Masur
March 28th, 2004, 06:13 AM
Originally posted by cb_schear
Also you need to delete the pointer to the thread (if you set it to auto delete false)
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...