diff --git a/binding/SkiaSharp/GRContext.cs b/binding/SkiaSharp/GRContext.cs index 918348f32d..273639ee15 100644 --- a/binding/SkiaSharp/GRContext.cs +++ b/binding/SkiaSharp/GRContext.cs @@ -91,6 +91,11 @@ public static GRContext CreateMetal (GRMtlBackendContext backendContext, GRConte var device = backendContext.DeviceHandle; var queue = backendContext.QueueHandle; +#if __IOS__ || __MACOS__ || __TVOS__ + using Foundation.NSObject queueObject = Foundation.NSObject.FromObject(backendContext.Queue); + queueObject.DangerousRetain (); +#endif + if (options == null) { return GetObject (SkiaApi.gr_direct_context_make_metal ((void*)device, (void*)queue)); } else { diff --git a/tests/SkiaSharp.Tests.Devices/Tests/iOS/iOSExtensionsTests.cs b/tests/SkiaSharp.Tests.Devices/Tests/iOS/iOSExtensionsTests.cs index a0e8c55f29..51398d4bed 100644 --- a/tests/SkiaSharp.Tests.Devices/Tests/iOS/iOSExtensionsTests.cs +++ b/tests/SkiaSharp.Tests.Devices/Tests/iOS/iOSExtensionsTests.cs @@ -1,5 +1,7 @@ -using CoreGraphics; +using System; +using CoreGraphics; using Foundation; +using Metal; using Xunit; namespace SkiaSharp.Views.iOS.Tests @@ -99,5 +101,21 @@ public void CGRectToSKRect(int x, int y, int w, int h) Assert.Equal(expected, actual); } + + [SkippableFact] + public void GRContextDisposeDoesNotCrash() + { + var device = MTLDevice.SystemDefault!; + Skip.If(device == null, "Metal is not supported on this device."); + + using var commandQueue = device.CreateCommandQueue(); + using var backendContext = new GRMtlBackendContext() + { + Device = device, + Queue = commandQueue, + }; + + using var context = GRContext.CreateMetal(backendContext); + } } }