Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 227749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:37:28+00:00 2026-05-11T19:37:28+00:00

// CMyDialog inherits from CDialog void CMyFrame::OnBnClickedCreate() { CMyDialog* dlg = new CMyDialog(); dlg->Create(

  • 0
// 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:37:29+00:00Added an answer on May 11, 2026 at 7:37 pm

    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 PostNcDestroy member function in your derived class. To add auto-cleanup to your class, call your base class and then do a delete this. To remove auto-cleanup from your class, call CWnd::PostNcDestroy directly instead of the PostNcDestroy member in your direct base class.

    void MyDialog::PostNcDestroy() 
    {
    
        CDialog::PostNcDestroy();
        delete this;
    }
    

    How this works (from MSDN):

    When destroying a Windows window, the
    last Windows message sent to the
    window is WM_NCDESTROY. The default
    CWnd handler for that message
    (CWnd::OnNcDestroy) will detach the
    HWND from the C++ object and call the
    virtual function PostNcDestroy. Some
    classes override this function to
    delete the C++ object.

    “delete this” will free any C++
    memory associated with the C++ object.
    Even though the default CWnd
    destructor calls DestroyWindow if
    m_hWnd is non-NULL, this does not lead
    to infinite recursion since the handle
    will be detached and NULL during the
    cleanup phase.

    You can also refer MSDN (Destroying Window Objects ) for further details.

    Note:

    This works for modeless dialog that can be allocated on the heap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CStdDlg thats inherits from a CDialog class. In the CStdDlg dialog,
I'm using a CMFCLinkCtrl in my custom dialog that inherits from CDialog. The CMFCLinkCtrl
I'm having problems with some code to create a CDialog based window. The code
I have a class which inherits from CListCtrl class, say class list. I have
Is there a way to retrieve window handle from a CDialog derived class (like
I try to create an area onto a CDialog, where I can put some
I got the two following CDialog-class created for creating dialog windows from templates and
I have a c++ class, MyDialog , deriving from CDialog , where I have
I am building MFC application where there is CDialog with child control derived from
I'm trying to create a simple modeless dialog box which I'm creating from my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.