Link to home
Start Free TrialLog in
Avatar of rickatseasoft
rickatseasoft

asked on

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
Avatar of Masta120
Masta120

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.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial