[Mac] Retain CFRunLoop while it is in use
authorthefiddler <stapostol@gmail.com>
Tue, 22 Jul 2014 14:20:46 +0000 (16:20 +0200)
committerthefiddler <stapostol@gmail.com>
Tue, 22 Jul 2014 14:20:46 +0000 (16:20 +0200)
Source/OpenTK/Platform/MacOS/HIDInput.cs

index 19c9d80..375e772 100755 (executable)
@@ -94,7 +94,7 @@ namespace OpenTK.Platform.MacOS
         readonly Dictionary<int, IntPtr> JoystickIndexToDevice =
             new Dictionary<int, IntPtr>();
 
-        readonly CFRunLoop RunLoop = CF.CFRunLoopGetMain();
+        readonly CFRunLoop RunLoop;
         readonly CFString InputLoopMode = CF.RunLoopModeDefault;
         readonly CFDictionary DeviceTypes = new CFDictionary();
 
@@ -118,6 +118,16 @@ namespace OpenTK.Platform.MacOS
         {
             Debug.Print("Using HIDInput.");
 
+            RunLoop = CF.CFRunLoopGetMain();
+            if (RunLoop == IntPtr.Zero)
+                RunLoop = CF.CFRunLoopGetCurrent();
+            if (RunLoop == IntPtr.Zero)
+            {
+                Debug.Print("[Error] No CFRunLoop found for {0}", GetType().FullName);
+                throw new InvalidOperationException();
+            }
+            CF.CFRetain(RunLoop);
+
             HandleDeviceAdded = DeviceAdded;
             HandleDeviceRemoved = DeviceRemoved;
             HandleDeviceValueReceived = DeviceValueReceived;
@@ -1776,6 +1786,11 @@ namespace OpenTK.Platform.MacOS
                         NativeMethods.IOHIDManagerClose(hidmanager, IOOptionBits.Zero);
                         hidmanager = IntPtr.Zero;
                     }
+
+                    if (RunLoop != IntPtr.Zero)
+                    {
+                        CF.CFRelease(RunLoop);
+                    }
                 }
                 else
                 {