Memory release in the non-Modal Dialog Box

Source: Internet
Author: User

For memory release in a non-modal subdialog box, the general solution isOnclose ()

AddDestroywindow ()

And thenPostncdestroy ()

MediumDelete this

.

 

To minimize the number of non-modal subdialogs that are generated, the subdialog box is displayed on the taskbar. When creating a subdialog box, the desktop is used as the parent window, as shown below:

Pdlg-> Create (idd_dialog_child,Getasktopwindow ()

);

 

In this case, if you close the non-modal subdialog box and then close the main dialog box, there is no problem. However, when you close the main dialog box, if there is another non-modal subdialog box mentioned above, it will cause memory leakage. The reason is that pdlg does not receive any closed messages at this time.

The solution is to save the pdlgcopy pointer of the non-modal subdialog box in the parent window. This pointer must be assigned when pdlg exists and be cleared when pdlg is destroyed. Then, when the main dialog box is closed, check the pointer. If it is not empty, release the pointer.

 

Now there are new problems. If you allocate memory for operations in pdlg, but you are in pdlgPostncdestroy ()

If it is released, the memory will still be leaked, because when the non-Modal Dialog Box pointer is deleted in the parent window, only the pdlg destructor will be imported.Postncdestroy ().

So either you release the memory in the Destructor or you call pdlgcopy-> destroywindow () in the parent window (). The former should note that this pointer and the object handle are both null, and whether the memory release in your child control depends on wm_destroy. The latter method should pay attention to your original destroywindow () the pdlgcopy operation is set to null. If the dialog box pointer is too many, you must use the template to save it. Pay attention to the problem that the iterator fails.

 

 

Why can't pdlg receive any closed messages?

Differences between the parent and owner of the window:

The following is an explanation in msdn:

Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 84190

Summary

In the Windows environment, the following two relationships can exist between Windows:

Owner-owned relationship. The owner-owned
Relationship determines which other windows are
Automatically destroyed when a window is destroyed. When
Window A is destroyed, Windows automatically destroys
All the windows that are owned by.

Parent-child relationship. The parent-child
Relationship determines where a window can be drawn on
The screen. A child window (that is, a window that
Has a parent) is confined to its parent window's
Client area.

This article discusses these
Relationships and some Windows functions that provide
Owner and parent information for a specified window.

 

The following example proves that "the owner-owned
Relationship determines which other windows are
Automatically destroyed when a window is destroyed. "It seems to be wrong.

 

// Receive the close message <br/> void cstaticmultidlg: onbuttoncreate () <br/>{< br/> // todo: add your control notification handler code here <br/> cdialogchild * pdlg = new cdialogchild; <br/> pdlg-> Create (idd_dialog_child, null ); <br/> pdlg-> showwindow (sw_show); <br/> cwnd * pparent = pdlg-> getparent (); // pparent0x00000000 {cwnd hwnd = ???} <Br/> cwnd * powner = pdlg-> getowner (); // powner0x00000000 {cwnd hwnd = ???} <Br/>}< br/> // you can receive the close message. <br/> void cstaticmultidlg: onbuttoncreate () <br/>{< br/> // todo: add your control notification handler code here <br/> cdialogchild * pdlg = new cdialogchild; <br/> pdlg-> Create (idd_dialog_child, this ); <br/> pdlg-> showwindow (sw_show); <br/> cwnd * pparent = pdlg-> getparent (); // pparent0x00000000 {cwnd hwnd = ???} <Br/> cwnd * powner = pdlg-> getowner (); // powner0x00000000 {cwnd hwnd = ???} <Br/>}</P> <p> // The message cannot be closed. <br/> void cstaticmultidlg: onbuttoncreate () <br/> {<br/> // todo: add your control notification handler code here <br/> cdialogchild * pdlg = new cdialogchild; <br/> pdlg-> Create (idd_dialog_child, getshorttopwindow (); <br/> pdlg-> setparent (getshorttopwindow ()); <br/> pdlg-> showwindow (sw_show); <br/> cwnd * PDES = getshorttopwindow (); // pdes0x00373458 {ctempwnd hwnd = 0x00010010} <br/> Cwnd * pparent = pdlg-> getparent (); // pparent0x00000000 {cwnd hwnd = ???} <Br/> cwnd * powner = pdlg-> getowner (); // powner0x00000000 {cwnd hwnd = ???} <Br/>}< br/> // The close message is not received. <br/> void cstaticmultidlg: onbuttoncreate () <br/>{< br/> // todo: add your control notification handler code here <br/> cdialogchild * pdlg = new cdialogchild; <br/> pdlg-> Create (idd_dialog_child, getdesktopwindow ()); <br/> pdlg-> setowner (this); <br/> pdlg-> showwindow (sw_show); <br/> cwnd * PDES = getshorttopwindow (); // pdes0x00373458 {ctempwnd hwnd = 0x00010010} <br/> cwnd * pparent = Pdlg-> getparent (); // pparent0x00000000 {cwnd hwnd = ???} <Br/> cwnd * powner = pdlg-> getowner (); // powner0x0012fdf4 {cstaticmultidlg hwnd = 0x001004f2 }== this <br/>}< br/> // In this case, the program will be suspended <br/> void cstaticmultidlg: onbuttoncreate () <br/> {<br/> // todo: add your control notification handler code here <br/> cdialogchild * pdlg = new cdialogchild; <br/> pdlg-> Create (idd_dialog_child, getshorttopwindow (); <br/> pdlg-> setparent (this); <br/> pdlg-> setowner (this ); <br/> Pdlg-> showwindow (sw_show); <br/> cwnd * PDES = get1_topwindow (); // pdes0x00373458 {ctempwnd hwnd = 0x00010010} <br/> cwnd * pparent = pdlg-> getparent (); // pparent0x00000000 {cwnd hwnd = ???} <Br/> cwnd * powner = pdlg-> getowner (); // powner0x0012fdf4 {cstaticmultidlg hwnd = 0x001004f2 }== this <br/>}

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.