Skip to content

Commit

Permalink
Merge pull request #6530 from smoogipoo/attempt-fix-macos-crashes
Browse files Browse the repository at this point in the history
Attempt to fix crashes when loading textures on macOS
  • Loading branch information
peppy authored Feb 18, 2025
2 parents a485da0 + 018fb73 commit f1c85f4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 20 deletions.
10 changes: 3 additions & 7 deletions osu.Framework/Platform/Apple/Native/NSAutoreleasePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -19,14 +20,9 @@ internal NSAutoreleasePool(IntPtr handle)
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()
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Platform/MacOS/MacOSClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override bool SetImage(Image image)
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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
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)}.");

Expand Down
14 changes: 3 additions & 11 deletions osu.Framework/Platform/MacOS/Native/NSImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ internal NSImage(IntPtr handle)
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);
}
}
}

0 comments on commit f1c85f4

Please sign in to comment.