diff --git a/src/OpenTK/OpenTK.csproj b/src/OpenTK/OpenTK.csproj
index 93e68005..bf4af5e4 100644
--- a/src/OpenTK/OpenTK.csproj
+++ b/src/OpenTK/OpenTK.csproj
@@ -137,6 +137,7 @@
+
diff --git a/src/OpenTK/Platform/MacOS/Cocoa/NSAutoreleasePool.cs b/src/OpenTK/Platform/MacOS/Cocoa/NSAutoreleasePool.cs
new file mode 100644
index 00000000..5e7e3ee0
--- /dev/null
+++ b/src/OpenTK/Platform/MacOS/Cocoa/NSAutoreleasePool.cs
@@ -0,0 +1,34 @@
+using System;
+
+namespace OpenTK.Platform.MacOS
+{
+ ///
+ /// The 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.
+ ///
+ public sealed class NSAutoreleasePool : IDisposable
+ {
+ private readonly IntPtr _autoreleasePool;
+
+ ///
+ /// Allocates and initializes a new .
+ ///
+ public NSAutoreleasePool()
+ {
+ _autoreleasePool = Cocoa.SendIntPtr(Class.NSAutoreleasePool, Selector.Get("alloc"));
+ _autoreleasePool = Cocoa.SendIntPtr(_autoreleasePool, Selector.Get("init"));
+ }
+
+ ///
+ /// Disposes of the instance, draining it.
+ ///
+ public void Dispose()
+ {
+ if (_autoreleasePool != IntPtr.Zero)
+ {
+ Cocoa.SendVoid(_autoreleasePool, Selector.Get("drain"));
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/OpenTK/Platform/MacOS/CocoaContext.cs b/src/OpenTK/Platform/MacOS/CocoaContext.cs
index 79958f63..850cd109 100644
--- a/src/OpenTK/Platform/MacOS/CocoaContext.cs
+++ b/src/OpenTK/Platform/MacOS/CocoaContext.cs
@@ -54,8 +54,6 @@ namespace OpenTK
"/System/Library/Frameworks/OpenGL.framework/OpenGLES",
AddImageFlags.ReturnOnError);
- private IntPtr autoreleasePool;
-
static CocoaContext()
{
Cocoa.Initialize();
@@ -119,10 +117,6 @@ namespace OpenTK
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
IntPtr pixelFormat = SelectPixelFormat(mode, majorVersion, minorVersion);
if (pixelFormat == IntPtr.Zero)
@@ -346,22 +340,16 @@ namespace OpenTK
Debug.Print("Disposing of Cocoa context.");
- if (!NSApplication.IsUIThread)
- {
- return;
- }
+ using (var pool = new NSAutoreleasePool())
+ {if (!NSApplication.IsUIThread)
+ { return;}
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("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;