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
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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