destroying child windows - Programmer
This is a discussion on destroying child windows - Programmer ; Hi,
I have a dialog-based application. The first step the user takes in the
application is to open a file using the File Dialog. When the user
clicks OK on this dialog, I create a new window using the Create
...
-
destroying child windows
Hi,
I have a dialog-based application. The first step the user takes in the
application is to open a file using the File Dialog. When the user
clicks OK on this dialog, I create a new window using the Create
method. The parent of the new window is the main dialog. Here is my
question. When the user clicks the X to close the main window, will
this automatically destroy the child window I have created, or must I
call DestroyWindow explicity on the child window?
Thanks,
LC
-
Re: destroying child windows
Thanks, that is what I thought. I am a bit confused though because I
recently downloaded a trial version of Bounds Checker and it is
complaining that I have resource leaks. The problem seems to be that I
am not explicitly calling DestroyWindow anywhere in my app. As a check,
I added the default OnDestroy handler to my child dialog, and I know
that it gets executed on shutdown. Could Bounds Checker be wrong?
-
Re: destroying child windows
"LC" wrote in message
news:1122991227.929084.217420@o13g2000cwo.googlegr oups.com...
> Thanks, that is what I thought. I am a bit confused though because I
> recently downloaded a trial version of Bounds Checker and it is
> complaining that I have resource leaks. The problem seems to be that I
> am not explicitly calling DestroyWindow anywhere in my app. As a check,
> I added the default OnDestroy handler to my child dialog, and I know
> that it gets executed on shutdown. Could Bounds Checker be wrong?
>
>
If your CWinApp::OnInitInstance() has something like:
m_pMainWnd = new YourCDialogDerivedClass;
then the problem is with the main dialog, not your child window. The
problem is the CDialog derives from CWnd, not CFrameWnd. CWnd-derived
objects don't do a "delete this" when the HWND is destroyed, assuming that
most CWnd-derived classes are allocated on the stack, and the stack cleanup
will delete the object automatically. Since in this case you're doing a
"new", YourCDialogDerivedClass isn't deleted by anyone.
To fix this, override YourDialogDerivedClass::OnNcDestroy() and do a "delete
this" there. WM_NCDESTROY is the last windows message posted to your
window, so it's safe to delete the object at that time.
Regards,
David
http://www.dcsoft.com