线程中创建一个窗口为什么会有内存泄漏?
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);
}

解决方案 »

  1.   

    UINT __stdcall ThreadProc(PVOID param)
    _beginthreadex(NULL, 0, ThreadProc, NULL, 0, NULL);
    这样搞就不会有内存泄露了。
      

  2.   

    可以在CAboutDlg里重写虚函数,写入delete this,这样动态对话框指针就可以释放
      

  3.   

    你搞错了,
    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; 
    }
      

  4.   

    觉得你的做法有问题。
    MSG msg;                            
        while(GetMessage (&msg, NULL, 0, 0 )) 
    {                                  
            
            TranslateMessage (&msg);        
            DispatchMessage (&msg); 
        } 
    这一段的意义是什么呢?CAboutDlg里面已经有消息循环了啊。
      

  5.   

    我觉得不用Create。直接DoModal就可以了。