Broke the autorelease code out into a class and replaced the usage.

This commit is contained in:
Jarl Gullberg 2017-07-09 23:13:32 +02:00
parent a7ddb543a9
commit 3cf07ff790
No known key found for this signature in database
GPG key ID: 750FF6F6BDA72D23
3 changed files with 40 additions and 17 deletions

View file

@ -137,6 +137,7 @@
<Compile Include="Platform\Egl\EglUnixContext.cs" /> <Compile Include="Platform\Egl\EglUnixContext.cs" />
<Compile Include="Platform\Egl\EglWinContext.cs" /> <Compile Include="Platform\Egl\EglWinContext.cs" />
<Compile Include="Platform\Egl\EglAnglePlatformFactory.cs" /> <Compile Include="Platform\Egl\EglAnglePlatformFactory.cs" />
<Compile Include="Platform\MacOS\Cocoa\NSAutoreleasePool.cs" />
<Compile Include="Platform\MappedGamePadDriver.cs" /> <Compile Include="Platform\MappedGamePadDriver.cs" />
<Compile Include="Platform\Windows\Bindings\HidProtocol.cs" /> <Compile Include="Platform\Windows\Bindings\HidProtocol.cs" />
<Compile Include="Platform\Windows\WinInputBase.cs" /> <Compile Include="Platform\Windows\WinInputBase.cs" />

View file

@ -0,0 +1,34 @@
using System;
namespace OpenTK.Platform.MacOS
{
/// <summary>
/// The <see cref="NSAutoreleasePool"/> class is a wrapper around the native objective-C NSAutoreleasePool.
/// In particular, this construct mimics the usage of an @autorelease block and can be used in much the same way,
/// only with a C# using block instead.
/// </summary>
public sealed class NSAutoreleasePool : IDisposable
{
private readonly IntPtr _autoreleasePool;
/// <summary>
/// Allocates and initializes a new <see cref="NSAutoreleasePool"/>.
/// </summary>
public NSAutoreleasePool()
{
_autoreleasePool = Cocoa.SendIntPtr(Class.NSAutoreleasePool, Selector.Get("alloc"));
_autoreleasePool = Cocoa.SendIntPtr(_autoreleasePool, Selector.Get("init"));
}
/// <summary>
/// Disposes of the <see cref="NSAutoreleasePool"/> instance, draining it.
/// </summary>
public void Dispose()
{
if (_autoreleasePool != IntPtr.Zero)
{
Cocoa.SendVoid(_autoreleasePool, Selector.Get("drain"));
}
}
}
}

View file

@ -54,8 +54,6 @@ namespace OpenTK
"/System/Library/Frameworks/OpenGL.framework/OpenGLES", "/System/Library/Frameworks/OpenGL.framework/OpenGLES",
AddImageFlags.ReturnOnError); AddImageFlags.ReturnOnError);
private IntPtr autoreleasePool;
static CocoaContext() static CocoaContext()
{ {
Cocoa.Initialize(); Cocoa.Initialize();
@ -119,10 +117,6 @@ namespace OpenTK
private void CreateContext(GraphicsMode mode, CocoaWindowInfo cocoaWindow, IntPtr shareContextRef, int majorVersion, int minorVersion, bool fullscreen) private void CreateContext(GraphicsMode mode, CocoaWindowInfo cocoaWindow, IntPtr shareContextRef, int majorVersion, int minorVersion, bool fullscreen)
{ {
// Create a new autorelease pool for allocations in this context (mach ports, etc)
autoreleasePool = Cocoa.SendIntPtr(Class.NSAutoreleasePool, Selector.Get("alloc"));
autoreleasePool = Cocoa.SendIntPtr(autoreleasePool, Selector.Get("init"));
// Prepare attributes // Prepare attributes
IntPtr pixelFormat = SelectPixelFormat(mode, majorVersion, minorVersion); IntPtr pixelFormat = SelectPixelFormat(mode, majorVersion, minorVersion);
if (pixelFormat == IntPtr.Zero) if (pixelFormat == IntPtr.Zero)
@ -346,22 +340,16 @@ namespace OpenTK
Debug.Print("Disposing of Cocoa context."); Debug.Print("Disposing of Cocoa context.");
if (!NSApplication.IsUIThread) using (var pool = new NSAutoreleasePool())
{ {if (!NSApplication.IsUIThread)
return; { return;}
}
if (IsCurrent) if (IsCurrent)
{ { Cocoa.SendVoid(NSOpenGLContext, Selector.Get("clearCurrentContext"));}
Cocoa.SendVoid(NSOpenGLContext, Selector.Get("clearCurrentContext"));
}
Cocoa.SendVoid(Handle.Handle, Selector.Get("clearDrawable")); Cocoa.SendVoid(Handle.Handle, Selector.Get("clearDrawable"));
Cocoa.SendVoid(Handle.Handle, Selector.Get("release")); Cocoa.SendVoid(Handle.Handle, Selector.Get("release"));
// Drain the autorelease pool for the context, closing mach ports (if there is one)
if (autoreleasePool != IntPtr.Zero)
{
Cocoa.SendVoid(autoreleasePool, Selector.Get("drain"));
} }
Handle = ContextHandle.Zero; Handle = ContextHandle.Zero;