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 :
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.
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.
--------------------------------------------------------------------------------
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.
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 ?
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.
Bookmarks