Merge pull request #6530 from smoogipoo/attempt-fix-macos-crashes

Attempt to fix crashes when loading textures on macOS
This commit is contained in:
Dean Herbert
2025-02-18 21:12:14 +09:00
committed by GitHub
4 changed files with 8 additions and 20 deletions

View File

@@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using JetBrains.Annotations;
namespace osu.Framework.Platform.Apple.Native
{
@@ -19,14 +20,9 @@ namespace osu.Framework.Platform.Apple.Native
private static readonly IntPtr sel_init = Selector.Get("init");
private static readonly IntPtr sel_drain = Selector.Get("drain");
[MustDisposeResource]
public static NSAutoreleasePool Init()
{
var pool = alloc();
Interop.SendIntPtr(pool.Handle, sel_init);
return pool;
}
private static NSAutoreleasePool alloc() => new NSAutoreleasePool(Interop.SendIntPtr(class_pointer, sel_alloc));
=> new NSAutoreleasePool(Interop.SendIntPtr(Interop.SendIntPtr(class_pointer, sel_alloc), sel_init));
public void Dispose()
{

View File

@@ -38,7 +38,7 @@ namespace osu.Framework.Platform.MacOS
using (NSAutoreleasePool.Init())
{
var nsData = NSData.FromBytes(stream.ToArray());
using var nsImage = NSImage.LoadFromData(nsData);
using var nsImage = NSImage.InitWithData(nsData);
return setToPasteboard(nsImage.Handle);
}
}

View File

@@ -28,7 +28,7 @@ namespace osu.Framework.Platform.MacOS
var bytesSpan = new Span<byte>(nativeData.MutableBytes, length);
stream.ReadExactly(bytesSpan);
using var nsImage = NSImage.LoadFromData(nativeData);
using var nsImage = NSImage.InitWithData(nativeData);
if (nsImage.Handle == IntPtr.Zero)
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");

View File

@@ -28,21 +28,13 @@ namespace osu.Framework.Platform.MacOS.Native
internal NSData TiffRepresentation => new NSData(Interop.SendIntPtr(Handle, sel_tiff_representation));
[MustDisposeResource]
internal static NSImage LoadFromData(NSData data)
{
var image = alloc();
Interop.SendIntPtr(image.Handle, sel_init_with_data, data);
return image;
}
internal void Release() => Interop.SendVoid(Handle, sel_release);
private static NSImage alloc() => new NSImage(Interop.SendIntPtr(class_pointer, sel_alloc));
internal static NSImage InitWithData(NSData data)
=> new NSImage(Interop.SendIntPtr(Interop.SendIntPtr(class_pointer, sel_alloc), sel_init_with_data, data));
public void Dispose()
{
if (Handle != IntPtr.Zero)
Release();
Interop.SendVoid(Handle, sel_release);
}
}
}