CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2003
    Posts
    14

    Killing child and parent windows

    Hi.

    I have a program that creates child windows (not dialogs)

    When the child window is closed (clicking on the X), ~method is called, which deletes allocated storage and sends a message to the parent window telling that the window was closed.

    Now in a situation, where the child window is up and the parent window is up, and we close the parent window, as much as I understand MFC, the parent window is killed before the child window is. So when the ~method of the child window is called, it sends a message to the parent window, but the parent window has already been (deleted? destroyed?) so the program just exits at that point. How can I avoid this or work around this or fix this?

    Thank you.

  2. #2
    Join Date
    Nov 2003
    Location
    Central Florida
    Posts
    44
    As far as I know MFC ensures that child windows unload before the parent, unless you are doing something "creative" like adding the child window at run time... or am I mistaken about that?
    But... but... I'll set the building on fire.

    How many keys do YOU press?

  3. #3
    Join Date
    Nov 2003
    Posts
    14
    My source was "Programming Windows with MFC second Edition" by Jeff Prosise.

    Chapter 12, page 721, says:

    "Child windows, on the other hand, are destroyed after their parents are destroyed."

    What he is talking about is toolbars, but the concept should still apply.

  4. #4
    Join Date
    Nov 2003
    Location
    Central Florida
    Posts
    44
    According to MSDN, calling DestroyWindow() will destroy all children first, then the parent window.
    But... but... I'll set the building on fire.

    How many keys do YOU press?

  5. #5
    Join Date
    Nov 2003
    Posts
    14
    So that would mean, when the parent window is killed, and it tries to destroy the child window, and a child window sends a message to the parent window, that should be accepted?

  6. #6
    Join Date
    Nov 2003
    Location
    Central Florida
    Posts
    44
    Yes, it should be. If memory serves, I've actually done that before Just don't count on the parent window hanging around long enough to receive and process the message unless you return 0 from your child window's DestroyWindow() proc.
    But... but... I'll set the building on fire.

    How many keys do YOU press?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured