Use returned value from init instead of alloc

See: https://developer.apple.com/documentation/objectivec/nsobject/1418641-init

> In some cases, a custom implementation of the `init()` method might
return a substitute object. You must therefore always use the object
returned by `init()`, and not the one returned by `alloc` or
`allocWithZone:`, in subsequent code.
This commit is contained in:
Dan Balasescu
2025-02-18 17:38:26 +09:00
parent a485da06e4
commit 7249d66349
4 changed files with 6 additions and 20 deletions

View File

@@ -20,13 +20,7 @@ namespace osu.Framework.Platform.Apple.Native
private static readonly IntPtr sel_drain = Selector.Get("drain");
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);
}
}
}