线程中创建一个窗口为什么会有内存泄漏?
UINT Ctest4Dlg::ThreadProc(PVOID param){
CAboutDlg dlg;
dlg.Create(IDD_ABOUTBOX,0);
dlg.ShowWindow(SW_SHOW);
//把以上几句都注释掉,还是会出现内存泄漏,说明是下面消息循环产生了内存泄漏,怎么修改?
MSG msg;
while(GetMessage (&msg, NULL, 0, 0 ))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
void Ctest4Dlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//OnOK();
::AfxBeginThread(ThreadProc,this->t5);
}
UINT Ctest4Dlg::ThreadProc(PVOID param){
CAboutDlg dlg;
dlg.Create(IDD_ABOUTBOX,0);
dlg.ShowWindow(SW_SHOW);
//把以上几句都注释掉,还是会出现内存泄漏,说明是下面消息循环产生了内存泄漏,怎么修改?
MSG msg;
while(GetMessage (&msg, NULL, 0, 0 ))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
void Ctest4Dlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//OnOK();
::AfxBeginThread(ThreadProc,this->t5);
}
_beginthreadex(NULL, 0, ThreadProc, NULL, 0, NULL);
这样搞就不会有内存泄露了。
CAboutDlg dlg;
这个不是new出来的,不需要实现
void CAboutDlg::PostNcDestroy(){
delete this;
}
就这样,还是有内存泄漏
UINT Ctest4Dlg::ThreadProc(PVOID param){ MSG msg;
while(GetMessage (&msg, NULL, 0, 0 ))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
} return 0;
}
MSG msg;
while(GetMessage (&msg, NULL, 0, 0 ))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
这一段的意义是什么呢?CAboutDlg里面已经有消息循环了啊。