From 00f46e400a896fe3e9c758b7b816bd780bcb030c Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Fri, 6 Sep 2024 17:19:03 +0200 Subject: [PATCH] Fix crash in AppHost::_QuitRequested (#17848) --- src/cascadia/WindowsTerminal/AppHost.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 7c538c71cd..86d70a4d81 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -1221,11 +1221,23 @@ void AppHost::_IsQuakeWindowChanged(const winrt::Windows::Foundation::IInspectab // Raised from our Peasant. We handle by propagating the call to our terminal window. void AppHost::_QuitRequested(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::Foundation::IInspectable&) { + const auto root = _windowLogic.GetRoot(); + if (!root) + { + return; + } + + const auto dispatcher = root.Dispatcher(); + if (!dispatcher) + { + return; + } + // We process the shutdown synchronously here, because otherwise the // AutomaticShutdownRequested() logic wouldn't run synchronously either. til::latch latch{ 1 }; - _windowLogic.GetRoot().Dispatcher().RunAsync(winrt::Windows::UI::Core::CoreDispatcherPriority::Normal, [&latch, weakThis = weak_from_this()]() { + dispatcher.RunAsync(winrt::Windows::UI::Core::CoreDispatcherPriority::Normal, [&latch, weakThis = weak_from_this()]() { const auto countDownOnExit = wil::scope_exit([&latch] { latch.count_down(); });