The C ++ non-modal dialog box is a good method for automatic destruction.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.