[MFC]比較安全的殺Modeless Dialog方法

在結束dialog之前,先在PostNcDestroy()內下PostMessage()或是SendMessage給()Parent Dialog來將pointer設成NULL。
之後再刪除this,這樣也比較不會發生modeless dialog內的destructor動作沒做完前,因Parent Dialog刪除modeless dialog而造成不定時的crash事件。

PostNcDestroy可以在IDDProperties裡面設定就可用了

Sample code
PostNcDestory
void CMDXBatchJobManagerDlg::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class

CDialog::PostNcDestroy();

::AfxGetMainWnd()->PostMessage(WM_MDLESSDLG_CLOSE);
//::AfxGetMainWnd()->SendMessage(WM_MDLESSDLG_CLOSE);

delete this;
}

OnPostMessage
LRESULT CMDXBatchJobManagerDlg::OnPostMessage(WPARAM wParam, LPARAM lParam)
{
m_pModelessDlg = NULL; //Set null. It will be delete by itself
return TRUE;
}


message map
in header file
#define WM_MDLESSDLG_CLOSE 10058 //Any unique value


in cpp
BEGIN_MESSAGE_MAP(CMDXBatchJobManagerDlg, CDialog)
ON_MESSAGE(WM_MDLESSDLG_CLOSE, PostNcDestroy)
END_MESSAGE_MAP()

No comments:

Post a Comment

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...