Is delete appropriate after a WM_CLOSE Message

Posted on 2006-04-07
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
Question by:rickatseasoft
    LVL 1

    Expert Comment

    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.
    LVL 43

    Accepted Solution

    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
      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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    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.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now