From 6d67c98e70fc3565d9f75c5a58b44cc38633f20b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 19 Oct 2016 20:39:12 -0700 Subject: [PATCH] Fixed crash on Mac OS X 10.10 and earlier --- src/video/cocoa/SDL_cocoakeyboard.m | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 91267e51b..8b2ed91c2 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -195,6 +195,11 @@ static IOHIDManagerRef s_hidManager = NULL; static void HIDCallback(void *context, IOReturn result, void *sender, IOHIDValueRef value) { + if (context != s_hidManager) { + /* An old callback, ignore it (related to bug 2157 below) */ + return; + } + IOHIDElementRef elem = IOHIDValueGetElement(value); if (IOHIDElementGetUsagePage(elem) != kHIDPage_KeyboardOrKeypad || IOHIDElementGetUsage(elem) != kHIDUsage_KeyboardCapsLock) { @@ -232,13 +237,14 @@ QuitHIDCallback() if (!s_hidManager) { return; } - IOHIDManagerUnscheduleFromRunLoop(s_hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - IOHIDManagerRegisterInputValueCallback(s_hidManager, NULL, NULL); - IOHIDManagerClose(s_hidManager, 0); #if 0 /* Releasing here causes a crash on Mac OS X 10.10 and earlier, * so just leak it for now. See bug 2157 for details. */ + IOHIDManagerUnscheduleFromRunLoop(s_hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + IOHIDManagerRegisterInputValueCallback(s_hidManager, NULL, NULL); + IOHIDManagerClose(s_hidManager, 0); + CFRelease(s_hidManager); #endif s_hidManager = NULL; @@ -267,7 +273,7 @@ InitHIDCallback() goto fail; } IOHIDManagerSetDeviceMatchingMultiple(s_hidManager, matches); - IOHIDManagerRegisterInputValueCallback(s_hidManager, HIDCallback, NULL); + IOHIDManagerRegisterInputValueCallback(s_hidManager, HIDCallback, s_hidManager); IOHIDManagerScheduleWithRunLoop(s_hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); if (IOHIDManagerOpen(s_hidManager, kIOHIDOptionsTypeNone) == kIOReturnSuccess) { goto cleanup;