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
526 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
ID: 17843587
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
ID: 17843811
Great answer, exactly what I needed.  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
pre4 challenge 19 94
sumHeights  challenge 17 66
Problem to event 3 79
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

20 Experts available now in Live!

Get 1:1 Help Now