A good way to automatically destroy the non-Modal Dialog Box
In non-Modal Dialog Box programming, because it needs to use a new dialog box object and then call the create function to create a dialog box, this involves the problem of when to delete the dialog box. The following method is used, this prevents you from worrying about memory leakage caused by the deletion of the dialog box.
1. Use classwizard to add the following virtual functions in the dialog box.
Virtual void postncdestroy ();
In this way, a function is available:
Void caboutdlg: postncdestroy ()
{
// Todo: add your specialized code here and/or call the base class
Delete this; // do you see this sentence? Is it very easy? Here we can directly Delete ourselves and release the memory
Cdialog: postncdestroy ();
}
Then, create a general non-modal dialog box:
Caboutdlg * pdlg = new caboutdlg ();
Pdlg-> Create (idd_about );
Pdlg-> showwindow (sw_show );
OK. So far, the non-modal dialog box has been created successfully, and you can write otherCodeNow, you don't have to worry about the memory leakage in this dialog box, because he will release his memory and see the above Delete this code? That is, the role.
Next, let's take a look at what msdn says:
Cwnd: postncdestroy
This method is called by the default onncdestroy method after the window has been destroyed. Derived classes can use this function for custom cleanup such as the deletion of the this pointer.
that is to say, this function is called when a form has been destroyed. It is the last function called by the form. In this function, you can perform some cleanup operations, such as the delete this pointer.