CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005
    Posts
    52

    Confused with directly deleting CWnd-derived object

    I've read this post, "Destroying Window Objects"
    http://msdn.microsoft.com/en-us/library/5zba4hah.aspx

    But that got me more confused.

    As it said, if I add auto-cleanup to my own CWnd-derived class, the cleanup should be,
    Code:
    CMyWnd* wnd = new CMyWnd;
    wnd->Create(...);
    
    wnd->DestroyWindow();
    now both c++ object and HWND get safely freed.

    Meanwhile, it presents that CWnd-derived object is not designed for auto-cleanup. If I wrote my own CWnd-derived class, allocated it on heap, then directly deleted it, for instance,

    Code:
    CMyWnd* wnd = new CMyWnd;
    wnd->Create(...);
    ...
    delete wnd;
    did the associated HWND get freed? Should I call DestroyWindow() in destructor of CMyWnd?
    It also said that
    For C++ Window objects that do not perform auto-cleanup, using the delete operator can cause a memory leak when you try to call DestroyWindow in the CWnd::~CWnd destructor if the VTBL does not point to the correctly derived class. This occurs because the system cannot find the appropriate destroy method to call. Using DestroyWindow instead of delete avoids these problems.
    So, here my question is, when I design a CMyWnd without overriding the PostNcDestroy, how could I safely get window object cleaned up?

    thanks.
    Last edited by LifeIsSuffering; April 21st, 2010 at 08:01 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Confused with directly deleting CWnd-derived object

    You don't need to call the destroywindow yourself. The only thing you need to do is delete the 'wnd' pointer and the resources are cleared for you.

    Code:
    CMyWnd* wnd = new CMyWnd;
    wnd->Create(...);
    ...
    delete wnd;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured