From 068159607e57b2a26aa9f3504ea3ec7841312bfa Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Mon, 22 May 2023 11:00:44 -0500 Subject: [PATCH] Leak the window when it closes on Windows 10 (#15397) Re: #15384 Basically, when we close a `DesktopWindowXamlSource`, it calls to `Windows_UI_Xaml!DirectUI::MetadataAPI::Reset`, which resets the XAML metadata provider _for the process_. So, closing one DWXS on one thread will force an A/V next time another thread tries to do something like... display a tooltip. Not immediately, but surely soon enough. This was fixed in Windows 11 by os.2020!5837001. That wasn't backported to Windows 10. This will cause a ~15MB memory leak PER WINDOW. OBVIOUSLY, this is bad, but it's less bad than crashing. We're gonna keep using #15384 for other ideas here too. (cherry picked from commit c589784b54d46187289651fa6c04c49fe05826aa) Service-Card-Id: 89283538 Service-Version: 1.18 --- src/cascadia/WindowsTerminal/IslandWindow.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp index 36b2243f5ed..82f28078f85 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.cpp +++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp @@ -42,6 +42,46 @@ IslandWindow::~IslandWindow() void IslandWindow::Close() { + static const bool isWindows11 = []() { + OSVERSIONINFOEXW osver{}; + osver.dwOSVersionInfoSize = sizeof(osver); + osver.dwBuildNumber = 22000; + + DWORDLONG dwlConditionMask = 0; + VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL); + + if (VerifyVersionInfoW(&osver, VER_BUILDNUMBER, dwlConditionMask) != FALSE) + { + return true; + } + return false; + }(); + + if (!isWindows11) + { + // BODGY + // ____ ____ _____ _______ __ + // | _ \ / __ \| __ \ / ____\ \ / / + // | |_) | | | | | | | | __ \ \_/ / + // | _ <| | | | | | | | |_ | \ / + // | |_) | |__| | |__| | |__| | | | + // |____/ \____/|_____/ \_____| |_| + // + // There's a bug in Windows 10 where closing a DesktopWindowXamlSource + // on any thread will free an internal static resource that's used by + // XAML for the entire process. This would result in closing window + // essentially causing the entire app to crash. + // + // To avoid this, leak the XAML island. We only need to leak this on + // Windows 10, since the bug is fixed in Windows 11. + // + // See GH #15384, MSFT:32109540 + auto a{ _source }; + winrt::detach_abi(_source); + + // + } + if (_source) { _source.Close();