Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
562 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

704 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