-
January 20th, 2004, 01:04 AM
#1
Destroy window ?
hi there,
I have a CEdit *m_pEdit pointer, it will be dynamically created using new and create fucntion and deleted using delete. My question is before deleting should we call the DestroyWindow function ? for ex :
m_pEdit->DestroyWindow();
delete m_pEdit;
m_pEdit = 0;
is the destroywindow necessary ?
My senior says that window will not be destroyed if not called DestroyWindow.
thanks ahead
rajs
-
January 20th, 2004, 01:52 AM
#2
Your senior is right.
Before calling delete you need to call DestroyWindow to destroy the window, created with Create method of CEdit class. CEdit class constructor do not do it by default.
-
January 20th, 2004, 02:10 AM
#3
thanks Armen for the reply
-
January 20th, 2004, 03:45 AM
#4
Originally posted by Armen
CEdit class constructor do not do it by default.
I'm sorry it should be "CEdit class destructor do not do it by default."...
-
January 20th, 2004, 11:54 AM
#5
Re: Destroy window ?
Originally posted by brraj
My senior says that window will not be destroyed if not called DestroyWindow.
The window will be destroyed if the parent is destroyed. It will also be destroyed if necessary by the destructor.
For example, it is common for a programmer to make the mistake of allocating a control in a function (instead of creating it with "new") and creating it in the function. Then they are confused and frustrated when they don't see the control in the dialog or other window. They don't see it because it gets destroyed when the function returns and the object goes out of scope and unallocated. The destructor destroys the window.
-
January 20th, 2004, 11:10 PM
#6
hi all,
thanks for the reply sam hobbs.
Armen what do u say for sam hobbs comments ?
cheers
rajs
-
January 21st, 2004, 02:09 AM
#7
Really I was wrong.
CEdit class destructor calls DestroyWindow of CWnd class.
You may see this with Spy++.
Last edited by Armen; January 21st, 2004 at 02:14 AM.
-
January 21st, 2004, 01:19 PM
#8
Thank you for the feedback. We seldom get positive feedback; we usually only get feedback when something does not work or is wrong.
-
January 22nd, 2004, 01:43 AM
#9
hi Armen & Sam Hobbs,
I am not only talking about just CEdit. i want to know if it is common for all the other ctrls too.. for ex- ccombobox..
thanks ahead
rajs
-
January 22nd, 2004, 03:38 AM
#10
Originally posted by brraj
I am not only talking about just CEdit. i want to know if it is common for all the other ctrls too.. for ex- ccombobox..
Yes, any class derived from CWnd will call DestroyWindow() from its dtor.
-
January 23rd, 2004, 12:35 AM
#11
hi gstercken ,
--------------------------------------------------------------------------------
Originally posted by gstercken
Yes, any class derived from CWnd will call DestroyWindow() from its dtor.
--------------------------------------------------------------------------------
If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?
You have always been supportive to me and thanks for that.
thanks
ahead
-
January 23rd, 2004, 01:09 AM
#12
Originally posted by brraj
If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?
You don't have to.
-
January 23rd, 2004, 01:36 AM
#13
hi gstercken ,
thanks for the info
rajeev
-
January 23rd, 2004, 03:23 PM
#14
Originally posted by brraj
If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?
You don't specify where in a modeless dialog you are refering to. I think you mean that in OnOk and/or OnCancel we must call DestroyWindow to end the dialog, and it is true that normally that is needed. Note that it is the dialog that is destroying itself, therefore we don't want to do something such as "delete this". According to the C++ standard (as best as I understand it) it is dangerous to do "delete this", but the danger is implementation-defined. Since VC is deveolped by Microsoft, who also develops Windows, and since Microsoft uses "delete this" in the PostNcDestroy virtual function, it is safe to assume we can do that. However we don't want to do "delete this" anywhere else! So how is our destructor going to be called if we want to destroy ourself? For modeless dialogs, we first destroy ourselves, and then delete ourselves from the universe when we get to our PostNcDestroy virtual function.
-
January 26th, 2004, 11:55 PM
#15
hi gstercken ,
what do u say about Sam Hobbs comments ?
cheers
rajs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|