学习多线程代码遇到内存泄露

来源:互联网 发布:windows与linux双系统 编辑:程序博客网 时间:2024/05/05 12:27

 

学习多线程http://www.vckbase.com/document/viewdoc/?id=1706 时

问题的发生:

创建新类 class CUIThread : public CWinThread 

protected://声明一个对象
CUIThreadDlg m_dlg; //界面窗口

BOOL CUIThread::InitInstance()
{
 m_dlg.Create(IDD_UITHREADDLG);
 m_dlg.ShowWindow(SW_SHOW);
 m_pMainWnd = &m_dlg;
 return TRUE;
}

int CUIThread::ExitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 m_dlg.DestroyWindow()
 return CWinThread::ExitInstance();
}

在CUIThreadDlg类中没有调用DestroyWindow();
//问题在这:ExitInstance在界面线程退出并不会调用到这个函数,导致 m_dlg对象不能销毁。内存泄露.

修改:在退出线程界面时调用 DestroyWindow();自己销毁对象因为是无模式对话框

void CUIThreadDlg::OnOK()
{
 // TODO: Add extra validation here
 CDialog::OnOK();
 DestroyWindow();
}

void CUIThreadDlg::OnCancel()
{
 // TODO: Add extra cleanup CDialog
 CDialog::OnCancel();
 DestroyWindow();
}

内存泄露得到解决

原创粉丝点击