An understanding of the pointer release of a modeless dialog box

Source: Internet
Author: User

Recent project Memory leak problem, find the modeless dialog box pointer release problem, especially many Layers dialog box release problem.

each CPP file plus:

#ifdef _DEBUG

#define NEW Debug_new

#endif

This code detects the new pointer and does not release the memory leak.

about the modeless dialog box pointer release problem, find some information, summarized as follows: The parent dialog box creates a dialog box with the pointer:

in the parent dialog box

definition: cdialog1* m_dialog1;//cdialog1* is a subclass of a child dialog box

Create and display:

M_dialog1 = new Cdialog1 ();
M_dialog1->create (Idd_dialog1, this);
M_dialog1->showwindow (sw_show);

where exactly is the release of M_dialog1 more reasonable.

here to understand some of the window mechanism, each window is closed (with Cdialog::onok () or Cdialog::oncancel ()), just hide the window, and did not destroy the window. So the window also executes the Cdialog::ondestroy () Destroy window, and once this is done, the window's handle is M_hwnd=null, and the destructor is finally executed. The process of closing and destroying windows is one such order.

do not believe that pull a window out to try it.

However, the child window is in the parent window with m_dialog1 = new Cdialog1 () out, the pointer object is in the heap (heap), must release, otherwise memory leaks, although the window handle in the closure of the entire program, Automatically ondestory () from the order of the parent window-child window, but the pointer object is not automatically destroyed. In short, the handle is gone, and the window pointer object is still (a handle is just a member of a lot of data in the pointer).

However, it is reasonable to release the right place.

Test 1:

in the Child dialog box Ondestory () calls delete this, releases the object, and the delete this triggers the destructor, which appears to be the same as the mechanism for destroying the window as described above, but will report an error:

Warning:calling DestroyWindow in Cdialog::~cdialog--ondestroy or PostNcDestroy in derived class won't be called.

The reason for this is that the Ondestory function destroys the handle after it has been returned, and if you do not go to delete this, the destructor is triggered, and a warning is reported without destroying the window before entering the destructor.

Test 2: We release the pointer object in the parent window because we're new here, and here's the delete right.

It is noted here that the dialog box based program main window is not a destructor, you can add an error, the reason is unknown.

Ctestptdlg is the class of the main dialog box, so place this in the Ondesotry () execution destroys the next level window and releases the pointer.

void Cteseptdlg::ondestroy ()
{
Cdialogex::ondestroy ();

TODO: Add Message Handler code here
if (m_dialog1!= NULL)
{
M_dialog1->destroywindow ();
HWND hmydialog1 = M_dialog1->getsafehwnd (); Here the Child dialog box handle must not have, fancy a sentence
Delete M_dialog1;
M_dialog1 = null;//this = delete
}

In addition to the main window is very special, other window release should be placed in the destructor.

Cdialog1::~cdialog1 ()
{
if (pdialog2!= NULL)
{
Pdialog2->destroywindow ()///here if there is a next level of child dialog box, will be automatically down a level destory
HWND hmydialog1 = Pdialog2->getsafehwnd (); Here the Child dialog box handle must not have, fancy a sentence
Delete pdialog2;
M_dialog1 = null;//this = delete
}
}

PDIALOG2 is the next level of CDialog dialog box, if we shut down the entire main window, the execution of Pdialog2->destroywindow () has been executed once, here again, the actual execution is unsuccessful, return false. But for the sake of unification, write it this way. This shows that the window can repeat Destorywindow (), but the pointer cannot repeat the delete.

There are also things to delete your own object in the class. It's best to send a message to the parent window to perform the destroy yourself and release the pointer instead of having to delete this in the class, unless your class is single mode.

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.