Solved

Will deleting a CWnd through it's own PostNcDestroy message cause a memory leak if it was allocated on the heap?

Posted on 2006-10-31
2
512 Views
Last Modified: 2013-11-20
I have a splash screen window that is allocated on the heap and deleted in it's PostNcDestroy message handler.  Will this cause a memory leak?

The reason I'm asking is because a memory leak detection program pointed to the spot where I create a new Window on the heap with out deleting it before the function ended.  It may be wrong in this case.

Details below, if needed:

It is created like this:

BOOL CMainFrame::ShowSplashScreen()
{            
      pSplash=new CSplashScreen();      

      pSplash->Create(this,_T("My Splash"),1700,CSS_FADE | CSS_CENTERSCREEN | CSS_SHADOW);

      pSplash->Show();
}

And deleted like this:

void CSplashScreen::PostNcDestroy()
{
      CWnd::PostNcDestroy();
      delete this;
}


This is preferable because the splash screen is killed as soon as a timer runs out:

void CSplashScreen::OnTimer(UINT nIDEvent)
{
      KillTimer(0);
      ShowWindow(SW_HIDE);
      DestroyWindow();
}
0
Comment
Question by:bigsteve87
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
This is absolutely OK, see http://msdn2.microsoft.com/zh-tw/library/5zba4hah.aspx resp. http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfcnotes_tn017.asp ("TN017: Destroying Window Objects"):

Auto Cleanup with CWnd::PostNcDestroy

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.

The default implementation of CWnd::PostNcDestroy does nothing which is appropriate for window objects allocated on the stack frame or embedded in other objects. This is not appropriate for window objects that are designed to be allocated by themselves on the heap (not embedded in other C++ object).

Those classes that are designed to be allocated by themselves on the heap override the PostNcDestroy member function to perform a "delete this". This statement 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
0
 

Author Comment

by:bigsteve87
Comment Utility
Great answer, exactly what I needed.  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now