From 633cd6e9dc3b20bbb57cf02dd216aabbf4567e91 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Thu, 15 Jan 2026 08:59:02 -0600 Subject: [PATCH] Wrap window shutdown in a top-level exception handler (#19743) (cherry picked from commit a0c79b59e6b6d89e4f013a784d3c890076d549a5) Service-Card-Id: PVTI_lADOAF3p4s4BBcTlzgjsATs Service-Version: 1.24 --- src/cascadia/WindowsTerminal/WindowEmperor.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp index 962a4ac726..5dea2594e9 100644 --- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp +++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp @@ -909,9 +909,18 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c // which would change the _windows array, and invalidate our iterator and crash. // // We can prevent this by deferring Close() until after the erase() call. + // + // Wrapping it in an exception handler will prevent us from losing track of the window count, at + // the perceived cost of leaking all the resources. However, the resources were being leaked + // anyway (since we threw and exited this message handler) so this at least gives back our + // deterministic window count management. const auto strong = *it; _windows.erase(it); - strong->Close(); + try + { + strong->Close(); + } + CATCH_LOG(); break; } }