Is delete appropriate after a WM_CLOSE Message

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
rickatseasoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Masta120Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.