From 32559695aa20d611c79579b022013849545b744a Mon Sep 17 00:00:00 2001 From: Tim Schneeberger Date: Sun, 4 Feb 2024 01:39:22 +0100 Subject: [PATCH] Prevent garbage collection of Win32 callback --- ThePBone.Interop.Win32/WndProcClient.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ThePBone.Interop.Win32/WndProcClient.cs b/ThePBone.Interop.Win32/WndProcClient.cs index d6143e3be..e23082657 100644 --- a/ThePBone.Interop.Win32/WndProcClient.cs +++ b/ThePBone.Interop.Win32/WndProcClient.cs @@ -12,11 +12,13 @@ public partial class WndProcClient public event EventHandler? MessageReceived; private readonly IntPtr _hwnd; + /* prevent garbage collection */ + private readonly Unmanaged.WNDCLASSEX _wndClassEx; public IntPtr WindowHandle => _hwnd; public WndProcClient() { - Unmanaged.WNDCLASSEX wndClassEx = new Unmanaged.WNDCLASSEX + _wndClassEx = new Unmanaged.WNDCLASSEX { cbSize = Marshal.SizeOf(), lpfnWndProc = delegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { @@ -36,7 +38,7 @@ public WndProcClient() lpszClassName = "MessageWindow " + Guid.NewGuid(), }; - ushort atom = Unmanaged.RegisterClassEx(ref wndClassEx); + ushort atom = Unmanaged.RegisterClassEx(ref _wndClassEx); if (atom == 0) {