From fee2ecf943042426c03fa4c1add437f283cc1914 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 11 Mar 2021 16:02:09 +0100 Subject: [PATCH] [CoreFoundation] Fix collected delegate in CFStream. (#10831) Fixes this fatal error at runtime: > A callback was made on a garbage collected delegate of type 'Xamarin.Mac!CoreFoundation.CFStream+CFStreamCallback::Invoke'. --- src/CoreFoundation/CFStream.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/CoreFoundation/CFStream.cs b/src/CoreFoundation/CFStream.cs index 2d0b0fcde7..a5fd172906 100644 --- a/src/CoreFoundation/CFStream.cs +++ b/src/CoreFoundation/CFStream.cs @@ -453,6 +453,8 @@ namespace CoreFoundation { stream.OnCallback ((CFStreamEventType) (long) type); } + static CFStreamCallback OnCallbackDelegate = OnCallback; + protected virtual void OnCallback (CFStreamEventType type) { var args = new StreamEventArgs (type); @@ -495,7 +497,7 @@ namespace CoreFoundation { var ptr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (CFStreamClientContext))); try { Marshal.StructureToPtr (ctx, ptr, false); - if (!DoSetClient (OnCallback, (nint) (long) args, ptr)) + if (!DoSetClient (OnCallbackDelegate, (nint) (long) args, ptr)) throw new InvalidOperationException ("Stream does not support async events."); } finally { Marshal.FreeHGlobal (ptr);