Skip to content

Commit 66c4b06

Browse files
authored
[CoreMedia] Store delegate in static variable to prevent premature garbage collection. (#10721)
Fixes this crash in monotouch-test: Process terminated. A callback was made on a garbage collected delegate of type 'Xamarin.Mac!CoreMedia.CMSampleBuffer+CMSampleBufferInvalidateCallback::Invoke'. Repeat 2 times: -------------------------------- at CoreMedia.CMSampleBuffer.CMSampleBufferInvalidate(IntPtr) -------------------------------- at CoreMedia.CMSampleBuffer.Invalidate() at MonoTouchFixtures.CoreMedia.SampleBufferTest.SetInvalidateCallback() at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean, Boolean) at NUnit.Framework.Internal.Reflect.InvokeMethod(System.Reflection.MethodInfo, System.Object, System.Object[]) at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(NUnit.Framework.Internal.TestExecutionContext) at NUnit.Framework.Internal.Execution.SimpleWorkItem.PerformWork() at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread() at NUnit.Framework.Internal.Execution.CompositeWorkItem.RunChildren() at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread() at NUnit.Framework.Internal.Execution.CompositeWorkItem.RunChildren() at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread() at NUnit.Framework.Internal.Execution.CompositeWorkItem.RunChildren() at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformWork() at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread() at NUnit.Framework.Internal.Execution.WorkItem.Execute() at NUnit.Framework.Internal.Execution.MainThreadWorkItemDispatcher.Dispatch(NUnit.Framework.Internal.Execution.WorkItem) at NUnit.Framework.Internal.Execution.CompositeWorkItem.RunChildren() at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformWork() at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread() at NUnit.Framework.Internal.Execution.WorkItem.Execute() at NUnit.Framework.Internal.Execution.MainThreadWorkItemDispatcher.Dispatch(NUnit.Framework.Internal.Execution.WorkItem) at NUnit.Framework.Internal.Execution.MainThreadWorkItemDispatcher.Start(NUnit.Framework.Internal.Execution.WorkItem) at NUnit.Framework.Api.NUnitTestAssemblyRunner.StartRun(NUnit.Framework.Interfaces.ITestListener) at NUnit.Framework.Api.NUnitTestAssemblyRunner+<>c__DisplayClass38_0.<RunAsync>b__0() at NUnit.Framework.Api.NUnitTestAssemblyRunner.WrapInNUnitCallContext(System.Action) at NUnit.Framework.Api.NUnitTestAssemblyRunner.RunAsync(NUnit.Framework.Interfaces.ITestListener, NUnit.Framework.Interfaces.ITestFilter) at NUnit.Framework.Api.NUnitTestAssemblyRunner.Run(NUnit.Framework.Interfaces.ITestListener, NUnit.Framework.Interfaces.ITestFilter) at MonoTouch.NUnit.UI.BaseTouchRunner.Run(NUnit.Framework.Internal.Test) at MonoTouch.NUnit.UI.BaseTouchRunner.Run() at MonoTouch.NUnit.UI.BaseTouchRunner.RunAsync() at MonoTouch.NUnit.UI.MacRunner+<RunTestsAsync>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Boolean, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at MonoTouch.NUnit.UI.MacRunner.RunTestsAsync(MonoTouch.NUnit.UI.TouchOptions, System.Reflection.Assembly[]) at MonoTouch.NUnit.UI.MacRunner+<>c__DisplayClass0_0+<<MainAsync>b__0>d.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at MonoTouch.NUnit.UI.MacRunner+<>c__DisplayClass0_0.<MainAsync>b__0() at Foundation.NSActionDispatcher.Apply() at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean, Boolean) at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo) at ObjCRuntime.Runtime.InvokeMethod(IntPtr, IntPtr, IntPtr) at ObjCRuntime.Runtime.bridge_runtime_invoke_method(IntPtr, IntPtr, IntPtr, IntPtr ByRef) at ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr_bool(IntPtr, IntPtr, IntPtr, IntPtr, Boolean) at ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr_bool(IntPtr, IntPtr, IntPtr, IntPtr, Boolean) at Foundation.NSObject.InvokeOnMainThread(System.Action) at MonoTouch.NUnit.UI.MacRunner+<MainAsync>d__0.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at MonoTouch.NUnit.UI.MacRunner.MainAsync(System.Collections.Generic.IList`1<System.String>, Boolean, System.Action`1<Int32>, System.Reflection.Assembly[]) at Xamarin.Mac.Tests.MainClass+<Main>d__0.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at Xamarin.Mac.Tests.MainClass.Main(System.String[]) at Xamarin.Mac.Tests.MainClass.<Main>(System.String[])
1 parent 861df21 commit 66c4b06

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/CoreMedia/CMSampleBuffer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ public CMSampleBufferError SetDataReady ()
599599
delegate void CMSampleBufferInvalidateCallback (/* CMSampleBufferRef */ IntPtr sbuf,
600600
/* uint64_t */ ulong invalidateRefCon);
601601

602+
static CMSampleBufferInvalidateCallback invalidate_handler = InvalidateHandler;
603+
602604
#if !MONOMAC
603605
[MonoPInvokeCallback (typeof (CMSampleBufferInvalidateCallback))]
604606
#endif
@@ -626,7 +628,7 @@ public CMSampleBufferError SetInvalidateCallback (Action<CMSampleBuffer> invalid
626628
return CMSampleBufferError.RequiredParameterMissing;
627629

628630
invalidate = GCHandle.Alloc (Tuple.Create (invalidateHandler, this));
629-
return CMSampleBufferSetInvalidateCallback (handle, InvalidateHandler, (ulong)(IntPtr)invalidate);
631+
return CMSampleBufferSetInvalidateCallback (handle, invalidate_handler, (ulong)(IntPtr)invalidate);
630632
}
631633

632634
[DllImport(Constants.CoreMediaLibrary)]

0 commit comments

Comments
 (0)