Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Is delete appropriate after a WM_CLOSE Message

Posted on 2006-04-07
2
Medium Priority
?
276 Views
Last Modified: 2013-11-20
I have some code that goes through some gyrations, but essentially, a dialog is created as the child of a MDI window using the new operator, and is ultimately closed with a WM_CLOSE message.  Following that close message, I am using the delete operator.  

Sometime, but not always, the code fails on the delete.  The literature is not exactly clear on the subject, but there are some implications that a child window is deleted automatically.  So I wonder if my problem is that sometimes the WM_CLOSE message arrives before the delete is called, and since the window is already deleted, the delete causes the error.  At other times, the delete occurs before the WM_CLOSE message arrives, so there are no problems.  Also, I should add that the immediate parent of the dialog is closing at the same time.  So, once again, if the parent is closing, is it reasonable to assume that the child dialog is automatically deleted as part of that process.

I guess that my question is should I remove the delete, or will that just lead to memory leaks, etc. later.

Thanks for your time, Rick
0
Comment
Question by:rickatseasoft
2 Comments
 
LVL 1

Expert Comment

by:Masta120
ID: 16406106
I would probably need to see your code to fully understand how you set up this dialog.

But, is there a reason you create this dialog object as a pointer and not as a regular variable?  If declared as a regular variable, you would not need to delete it.  It will get removed when the variable goes out of scope (and no memory leaks).

If you need it to be a pointer to a dlg object, try to only create it (create it with a new command) in the constructor of the class where it is launched.  Then you can delete it in the destructor.

If you launch this dialog multiple times, you can just keep using the pointer that was created in the constructor over and over again.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 16406254
Two separate things.
The WM_CLOSE message is to allow windows to release handles and close the window objetcs.
The delete is to release memory.

Some windows the programmer is 'clever'.  You might see code like
CMyDlg::PostNCDestroy()
{
  delete this;
}

here the PostNCDestroy is called as part of the closing of the indow (WM_CLOSE) but you see it also has delete this - this tidies the memory up.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

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.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

572 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