Skip to content

Commit f1c85f4

Browse files
authored
Merge pull request ppy#6530 from smoogipoo/attempt-fix-macos-crashes
Attempt to fix crashes when loading textures on macOS
2 parents a485da0 + 018fb73 commit f1c85f4

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

osu.Framework/Platform/Apple/Native/NSAutoreleasePool.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using System;
5+
using JetBrains.Annotations;
56

67
namespace osu.Framework.Platform.Apple.Native
78
{
@@ -19,14 +20,9 @@ internal NSAutoreleasePool(IntPtr handle)
1920
private static readonly IntPtr sel_init = Selector.Get("init");
2021
private static readonly IntPtr sel_drain = Selector.Get("drain");
2122

23+
[MustDisposeResource]
2224
public static NSAutoreleasePool Init()
23-
{
24-
var pool = alloc();
25-
Interop.SendIntPtr(pool.Handle, sel_init);
26-
return pool;
27-
}
28-
29-
private static NSAutoreleasePool alloc() => new NSAutoreleasePool(Interop.SendIntPtr(class_pointer, sel_alloc));
25+
=> new NSAutoreleasePool(Interop.SendIntPtr(Interop.SendIntPtr(class_pointer, sel_alloc), sel_init));
3026

3127
public void Dispose()
3228
{

osu.Framework/Platform/MacOS/MacOSClipboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override bool SetImage(Image image)
3838
using (NSAutoreleasePool.Init())
3939
{
4040
var nsData = NSData.FromBytes(stream.ToArray());
41-
using var nsImage = NSImage.LoadFromData(nsData);
41+
using var nsImage = NSImage.InitWithData(nsData);
4242
return setToPasteboard(nsImage.Handle);
4343
}
4444
}

osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
2828
var bytesSpan = new Span<byte>(nativeData.MutableBytes, length);
2929
stream.ReadExactly(bytesSpan);
3030

31-
using var nsImage = NSImage.LoadFromData(nativeData);
31+
using var nsImage = NSImage.InitWithData(nativeData);
3232
if (nsImage.Handle == IntPtr.Zero)
3333
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");
3434

osu.Framework/Platform/MacOS/Native/NSImage.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,13 @@ internal NSImage(IntPtr handle)
2828
internal NSData TiffRepresentation => new NSData(Interop.SendIntPtr(Handle, sel_tiff_representation));
2929

3030
[MustDisposeResource]
31-
internal static NSImage LoadFromData(NSData data)
32-
{
33-
var image = alloc();
34-
Interop.SendIntPtr(image.Handle, sel_init_with_data, data);
35-
return image;
36-
}
37-
38-
internal void Release() => Interop.SendVoid(Handle, sel_release);
39-
40-
private static NSImage alloc() => new NSImage(Interop.SendIntPtr(class_pointer, sel_alloc));
31+
internal static NSImage InitWithData(NSData data)
32+
=> new NSImage(Interop.SendIntPtr(Interop.SendIntPtr(class_pointer, sel_alloc), sel_init_with_data, data));
4133

4234
public void Dispose()
4335
{
4436
if (Handle != IntPtr.Zero)
45-
Release();
37+
Interop.SendVoid(Handle, sel_release);
4638
}
4739
}
4840
}

0 commit comments

Comments
 (0)