Merge pull request #624 from Nihlus/cocoa-machport-leak-fix

Fix leaking Cocoa Machports on macOS
This commit is contained in:
Fraser Waters 2017-08-06 18:11:09 +01:00 committed by GitHub
commit e331a6d5ca
4 changed files with 44 additions and 4 deletions

View file

@ -137,6 +137,7 @@
<Compile Include="Platform\Egl\EglUnixContext.cs" />
<Compile Include="Platform\Egl\EglWinContext.cs" />
<Compile Include="Platform\Egl\EglAnglePlatformFactory.cs" />
<Compile Include="Platform\MacOS\Cocoa\NSAutoreleasePool.cs" />
<Compile Include="Platform\MappedGamePadDriver.cs" />
<Compile Include="Platform\Windows\Bindings\HidProtocol.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()
{
var uninitializedPool = Cocoa.SendIntPtr(Class.NSAutoreleasePool, Selector.Alloc);
_autoreleasePool = Cocoa.SendIntPtr(uninitializedPool, Selector.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

@ -345,12 +345,16 @@ namespace OpenTK
return;
}
using (var pool = new NSAutoreleasePool())
{
if (IsCurrent)
{
Cocoa.SendVoid(NSOpenGLContext, Selector.Get("clearCurrentContext"));
}
Cocoa.SendVoid(Handle.Handle, Selector.Get("clearDrawable"));
Cocoa.SendVoid(Handle.Handle, Selector.Get("release"));
}
Handle = ContextHandle.Zero;

View file

@ -137,6 +137,7 @@ namespace OpenTK.Platform.MacOS
}
private CocoaWindowInfo windowInfo;
private IntPtr windowClass;
private IntPtr trackingArea;
private IntPtr current_icon_handle;