From aabd038e88951a992124ddfcb2be06ee6daaf4a5 Mon Sep 17 00:00:00 2001 From: Ryan Newington Date: Fri, 4 Oct 2024 17:52:12 +1000 Subject: [PATCH] Fixes an issue that can cause Avalonia-based applications to crash. The GetMessage call in RunLoop() of WinUiCompositorConnection.cs has been updated to process the result of GetMessage, and terminate the message loop if a WM_QUIT message is received on the pump. (#17190) Presently the WM_QUIT message is ignored and the loop re-enters the GetMessage call, causing an unmanaged exception and process crash. https://github.com/AvaloniaUI/Avalonia/issues/17188 --- .../WinRT/Composition/WinUiCompositorConnection.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs index 5a8360b515..11721cd47e 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs @@ -161,12 +161,20 @@ internal class WinUiCompositorConnection : IRenderTimer, Win32.IWindowsSurfaceFa return UnmanagedMethods.DefWindowProc(hwnd, msg, w, l); }); UnmanagedMethods.SetTimer(dw.Handle, IntPtr.Zero, 1000, null); - while (!cts.IsCancellationRequested) + + var result = 0; + while (!cts.IsCancellationRequested + && (result = UnmanagedMethods.GetMessage(out var msg, IntPtr.Zero, 0, 0)) > 0) { - UnmanagedMethods.GetMessage(out var msg, IntPtr.Zero, 0, 0); lock (_shared.SyncRoot) UnmanagedMethods.DispatchMessage(ref msg); } + + if (result < 0) + { + Logger.TryGet(LogEventLevel.Error, "WinUIComposition") + ?.Log(this, "Unmanaged error in {0}. Error Code: {1}", nameof(RunLoop), Marshal.GetLastWin32Error()); + } } public static bool IsSupported()