Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:43:21+00:00 2026-05-11T14:43:21+00:00

ATL CWindow class has a useful virtual method OnFinalMessage which is called after the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:43:21+00:00Added an answer on May 11, 2026 at 2:43 pm

    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; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I'm trying to call the Win32 version of SetWindowPos from within an ATL class
Modern ATL/MFC applications now have access to a new shared pointer class called CAutoPtr,
Without: ATL MFC Note: Client is executing in a different thread to that of
Without: ATL MFC Note: Plain C++ Out-of-process COM Object/server Predefined TLB file Question: How

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.