tags:

views:

192

answers:

2

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?

+3  A: 

PostNcDestroy() 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.

John Dibling
Hope that helped. If it did, please accept this answer. Thanks!
John Dibling
I'm sorry to say it did not. It seems that PostNcDestroy is not the same thing as OnFinalMessage. OnFinalMessage is called just before the last invocation of the window procedure for the window finishes - on the other hand PostNcDestroy is called when the WM_NC_DESTROY message is received
1800 INFORMATION
But, if the window is destroyed while in the process of handling another window message, the PostNcDestroy will come before the other window message handler is finished
1800 INFORMATION
WM_NC_DESTROY is supposed to be the last message a window ever receives, and is sent by the framework as a result of the window being closed. (ie from a call to DestroyWindow()). Are you seeing otherwise?
John Dibling
It is the last window message a window receives - it is just called before the end of the last invocation of the window procedure
1800 INFORMATION
See for example http://support.microsoft.com/?kbid=202110 for an explanation of the problem
1800 INFORMATION
The ATL CWindow procedure uses the OnFinalMessage as a good solution for this - it is designed to handle recursive invocations of the window procedure, and it is not called until the last one still on the stack has finished
1800 INFORMATION
+1  A: 

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.

  1. DialogProc() is called with WM_CLOSE.
  2. ProcessWindowMessage() calls your WM_CLOSE handler.
  3. In your WM_CLOSE handler, you call DestroyWindow().
  4. This ends up calling DialogProc again with WM_NCDESTROY.
  5. ProcessWindowMessage() calls your WM_NCDESTROY handler.
  6. 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;
}
1800 INFORMATION