Teen Programmers Unite  


Return to forum top

window closing under win32 and mfc

Posted by pramod [send private reply] at January 15, 2003, 12:35:15 PM

i was fooling with this program of mine. well, it had this dialog, which creates a few child dialogs.

now when i click the close button on the parent dialog with more than one child window open, only one child window gets close.

i also have a close button of my own [meaning a command button with 'close' written over it]. that has code somethign like this :

CWhatever::OnCloseButton() {

CWhatever::KillChildWindows() {
EnumChildWindows((HWND)this, CWhatever::JackRipper, NULL);

// this is static all right
CWhatever::JackRipper(HWND hwnd, LPARAM lP) {

CWhatever::OnClose() {
// my own cleanup
// more of my cleanup

now when i use this close button, the program crashes. and it crashes some where inside the win32/MFC arch. from where i think, this probably has something to do with trying to access objects i've probably already killed.

Question is -
1. What's going wrong?
2. Whats the difference between CloseWindow [seems to do some kind of dialog minimize], DestroyWindow [doesn't seem to call WM_CLOSE], EndDialog [can I use this for modeless windows?]?

Posted by CViper [send private reply] at January 15, 2003, 12:49:21 PM

From the Platform SDK:

The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it and remove the keyboard focus from it. The function also destroys the window's menu, flushes the thread message queue, destroys timers, removes clipboard ownership, and breaks the clipboard viewer chain (if the window is at the top of the viewer chain).

If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.
The CloseWindow function minimizes (but does not destroy) the specified window.

That means, you want to use DestroyWindow(); but you do not have to call it on the child windows (my guess is, that you app crashes when the parent-window tries to access resources destroyed by you [edit]but you already said that :D[/edit])

Btw: you don't seem to call JackRipper() on your main window at all...

Posted by pramod [send private reply] at January 17, 2003, 12:32:12 PM

you're right.

Actually, I was worried about the memory leaks when I allocated windows on the heap with something like :

CChildDialog* childDlg = new CChildDialog(this);

So, I was like putting them[the pointers] into a vector and then deleting the pointers. [This was I think what made the app crash, and I also show that in the code on the first post]. Turns out there's an easier way to this, get the window delete itself when it gets PostNcDestroy().

Posted by pramod [send private reply] at January 17, 2003, 12:33:02 PM

didn't show in the post, I mean

Posted by CViper [send private reply] at January 17, 2003, 02:31:38 PM

just funny the CChildDialog destructor didn't properly delete the child window :/ Bad design on M$ part :)

Posted by buzgub [send private reply] at January 17, 2003, 08:15:19 PM

http://winprog.org has an FAQ that talks about the difference between the WM_CLOSE and WM_DESTROY messages, if that's what you want to know about.

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.