Confused with directly deleting CWnd-derived object
I've read this post, "Destroying Window Objects"
http://msdn.microsoft.com/en-us/library/5zba4hah.aspx
But that got me more confused.
As it said, if I add auto-cleanup to my own CWnd-derived class, the cleanup should be,
Code:
CMyWnd* wnd = new CMyWnd;
wnd->Create(...);
wnd->DestroyWindow();
now both c++ object and HWND get safely freed.
Meanwhile, it presents that CWnd-derived object is not designed for auto-cleanup. If I wrote my own CWnd-derived class, allocated it on heap, then directly deleted it, for instance,
Code:
CMyWnd* wnd = new CMyWnd;
wnd->Create(...);
...
delete wnd;
did the associated HWND get freed? Should I call DestroyWindow() in destructor of CMyWnd?
It also said that
Quote:
For C++ Window objects that do not perform auto-cleanup, using the delete operator can cause a memory leak when you try to call DestroyWindow in the CWnd::~CWnd destructor if the VTBL does not point to the correctly derived class. This occurs because the system cannot find the appropriate destroy method to call. Using DestroyWindow instead of delete avoids these problems.
So, here my question is, when I design a CMyWnd without overriding the PostNcDestroy, how could I safely get window object cleaned up?
thanks.
Re: Confused with directly deleting CWnd-derived object
You don't need to call the destroywindow yourself. The only thing you need to do is delete the 'wnd' pointer and the resources are cleared for you.
Code:
CMyWnd* wnd = new CMyWnd;
wnd->Create(...);
...
delete wnd;