From bf392162ab0cd6886e050bb3bc796b346509f72f Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Sat, 24 Oct 2026 19:01:51 -0400 Subject: [PATCH] fix(#989): stop the use-after-free crash when the Windows app closes Crash A of #984. windowManager.destroy() in window_manager 0.5.2 only posts WM_QUIT, so WM_DESTROY never fires and OnDestroy never nulls the controller. The empty ~FlutterWindow then let ~unique_ptr delete the controller without nulling it first; a message arriving mid-teardown (accessibility, text input) went through MessageHandler into the half-destroyed view and FlutterWindowsView::GetEngine read freed memory. Symbolized from crash dumps at flutter_windows.dll+0x1d7b0. - ~FlutterWindow moves the controller out before destroying it, so the MessageHandler guard holds on every quit path. - main.cpp scopes the window so it is destroyed before CoUninitialize. Crash B (BLE plugin unhandled C++ exception) and the link drops stay on #984. Co-Authored-By: Claude Opus 5.5 --- windows/runner/flutter_window.cpp | 22 ++++++++++++++++++++- windows/runner/main.cpp | 32 +++++++++++++++++++------------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/windows/runner/flutter_window.cpp b/windows/runner/flutter_window.cpp index 9eda059a..5560cc16 100644 --- a/windows/runner/flutter_window.cpp +++ b/windows/runner/flutter_window.cpp @@ -11,7 +11,27 @@ FlutterWindow::FlutterWindow(const flutter::DartProject& project) : project_(project) {} -FlutterWindow::~FlutterWindow() {} +FlutterWindow::~FlutterWindow() { + // Destroy the controller with the member ALREADY null. (#984) + // + // This destructor is reached without OnDestroy() having run whenever the + // message loop ends before the window is destroyed. windowManager.destroy() + // does exactly that: in window_manager 0.5.2, Destroy() is only + // PostQuitMessage(0), so WM_DESTROY is never sent and OnDestroy()'s safe + // reset never happens. + // + // Left to the member destructor, the controller was deleted by + // ~unique_ptr, which in MSVC does NOT null the pointer first (reset() does). + // While the engine tore down, the still-live top-level window received a + // message, MessageHandler saw a non-null flutter_controller_, and forwarded + // into the half-destroyed view: FlutterWindowsView::GetEngine read freed + // memory and the app crashed on close. + // + // Moving it out leaves flutter_controller_ null for the whole teardown, so + // MessageHandler's guard holds no matter which path ended the loop. + auto controller = std::move(flutter_controller_); + controller.reset(); +} bool FlutterWindow::OnCreate() { if (!Win32Window::OnCreate()) { diff --git a/windows/runner/main.cpp b/windows/runner/main.cpp index baf81c41..ad06e597 100644 --- a/windows/runner/main.cpp +++ b/windows/runner/main.cpp @@ -24,18 +24,26 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - FlutterWindow window(project); - Win32Window::Point origin(10, 10); - Win32Window::Size size(1280, 720); - if (!window.Create(L"Offband Meshcore", origin, size)) { - return EXIT_FAILURE; - } - window.SetQuitOnClose(true); - - ::MSG msg; - while (::GetMessage(&msg, nullptr, 0, 0)) { - ::TranslateMessage(&msg); - ::DispatchMessage(&msg); + // Scoped so the window, and the engine inside it, is destroyed BEFORE + // CoUninitialize below. Accessibility (UIAutomationCore) and text input + // (msctf) are COM-based and both sit on the #984 crash stack: tearing them + // down after COM has been uninitialized is undefined behaviour. Previously + // `window` was a plain local, destroyed by the return statement, i.e. after + // CoUninitialize had already run. (#984) + { + FlutterWindow window(project); + Win32Window::Point origin(10, 10); + Win32Window::Size size(1280, 720); + if (!window.Create(L"Offband Meshcore", origin, size)) { + return EXIT_FAILURE; + } + window.SetQuitOnClose(true); + + ::MSG msg; + while (::GetMessage(&msg, nullptr, 0, 0)) { + ::TranslateMessage(&msg); + ::DispatchMessage(&msg); + } } ::CoUninitialize();