// CMyDialog inherits from CDialog
void CMyFrame::OnBnClickedCreate()
{
CMyDialog* dlg = new CMyDialog();
dlg->Create( IDD_MYDIALOG, m_thisFrame );
dlg->ShowWindow( SW_SHOW );
}
I’m pretty sure this leaks. What I’m really asking is: is there any “magic” in MFC that does dialog cleanup when the dialog is destroyed. How would it work if dlg wasn’t a pointer but declared on the stack – wouldn’t the destructor destroy the window when dlg goes out of scope.
Yes, it is memory leak in your case but you can avoid memory leak in cases where modeless dialog allocated on the heap by making use of overriding
PostNcDestroy.Dialogs are not designed for auto-cleanup ( where as Main frame windows, View windows are).
In case you want to provide the auto-cleanup for dialogs then you must override the
PostNcDestroymember function in your derived class. To add auto-cleanup to your class, call your base class and then do adelete this. To remove auto-cleanup from your class, callCWnd::PostNcDestroydirectly instead of thePostNcDestroymember in your direct base class.How this works (from MSDN):
You can also refer MSDN (Destroying Window Objects ) for further details.
Note:
This works for modeless dialog that can be allocated on the heap.