From 44c31d2700874ece57bcb7dcd3b7a56f1c5b820a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 11 Dec 2017 23:07:33 +0100 Subject: [PATCH] Destroy MFC main window when its wx counterpart is destroyed This avoids using m_pMainWnd after its HWND becomes invalid, as this resulted in assert failures from CWnd::WalkPreTranslateTree() called with this HWND as its hWndStop argument from PreTranslateMessage() which was used to pre-translate a WM_NULL message the application sometimes received while closing down. --- include/wx/msw/mfc.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/wx/msw/mfc.h b/include/wx/msw/mfc.h index f3fc23e44d..14b1a1d5d4 100644 --- a/include/wx/msw/mfc.h +++ b/include/wx/msw/mfc.h @@ -122,6 +122,11 @@ protected: // running. m_pMainWnd = new wxMFCWnd(w); + // We also need to reset m_pMainWnd when this window will be destroyed + // to prevent MFC from using an invalid HWND, which is probably not + // fatal but can result in at least asserts failures. + w->Bind(wxEVT_DESTROY, &wxMFCApp::OnMainWindowDestroyed, this); + // And we need to let wxWidgets know that it should exit the // application when this window is closed, as OnRun(), which does this // by default, won't be called when using MFC main message loop. @@ -129,6 +134,15 @@ protected: return TRUE; } + +private: + void OnMainWindowDestroyed(wxWindowDestroyEvent& event) + { + event.Skip(); + + delete m_pMainWnd; + m_pMainWnd = NULL; + } }; typedef wxMFCApp wxMFCWinApp;