ATL CWindow class has a useful virtual method OnFinalMessage
which is called after the last window message for the window is processed - at this point it is safe to destroy or deleted any objects associated with the window. Is there any equivalent for windows derived from the MFC CWnd
class?
views:
192answers:
2PostNcDestroy() is what you're looking for.
By the way, if you're implementing a modeless dialog and are looking for where to "delete this;", PostNcDestroy() is the place.
This answer describes how I eventually solved my problem. I'll note that while the answer by John Dibling was helpful, this was not the final solution to my problem. This is because the WM_NC_DESTROY message is sent as the final message to the window, but this can be handled before the last message to the window has finished being handled. See for example http://support.microsoft.com/?kbid=202110 for an explanation of the problem.
- DialogProc() is called with WM_CLOSE.
- ProcessWindowMessage() calls your WM_CLOSE handler.
- In your WM_CLOSE handler, you call DestroyWindow().
- This ends up calling DialogProc again with WM_NCDESTROY.
- ProcessWindowMessage() calls your WM_NCDESTROY handler.
- You call "delete this" in your WM_NCDESTROY handler.
After having called delete this
, the object is no longer valid, but you are still technically in the WM_CLOSE
handler so you will probably crash when you eventually get back there. This means it is not really safe to assume that you can do delete this
in PostNcDestroy, since the object may still be live in some other stack frame.
///
/// A window designed to allow any window to use the "OnFinalMessage" method from the ATL CWindow class
/// You must call SubclassWindow for this instance so that the window procedure runs
template<class T>
class FinalMessageWindow : public CWindowImpl<FinalMessageWindow<T> >
{
T *_t; /// The object wanting to receive the final message notification
public:
BEGIN_MSG_MAP(FinalMessageWindow<T>)
END_MSG_MAP()
///
/// The constructor
/// \param t The object that wants to get the OnFinalMessage notification
FinalMessageWindow(T *t)
: _t(t)
{
}
///
/// Called when the final window message for the window has been processed - this is often a good time to delete the object
/// \param hWnd The window handle
virtual void OnFinalMessage(HWND hWnd)
{
_t->OnFinalMessage(hWnd);
}
};
I created the above class, note that it is derived from the ATL CWindow class - this allows me to use the OnFinalMessage handler for this class. The OnFinalMessage handler is different from the PostNcDestroy in MFC windows in that it is guaranteed to be called only after the final message handler on the stack has completed.
We then use window subclassing to insert this window as the window procedure for my own window:
// roughly speaking
FinalMessageWindow<MyWindow> _finalMessageWindow(this);
finalMessageWindow.SubclassWindow(m_hWnd);
Then we implement the OnFinalMessage handler for our window:
void MyWindow::OnFinalMessage(HWND hWnd)
{
delete this;
}