Skip to content

Fix crash disposing GRContext for Metal backend #3258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions binding/SkiaSharp/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion tests/SkiaSharp.Tests.Devices/Tests/iOS/iOSExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CoreGraphics;
using System;
using CoreGraphics;
using Foundation;
using Metal;
using Xunit;

namespace SkiaSharp.Views.iOS.Tests
Expand Down Expand Up @@ -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);
}
}
}