mirror of
https://github.com/SK-la/osu-framework.git
synced 2026-03-15 03:20:30 +00:00
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:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)}.");
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user