cocoabits / MASShortcut

Modern framework for managing global keyboard shortcuts compatible with Mac App Store. More details:
http://blog.shpakovski.com/2012/07/global-keyboard-shortcuts-in-cocoa.html
BSD 2-Clause "Simplified" License
1.52k stars 220 forks source link

Maybe found a potential memory leak of the elements in globalHotKeys #50

Closed torinkwok closed 9 years ago

torinkwok commented 9 years ago

When I hit the hot keys composed of ⌃⌘Space (a system-wide shortcut), isTakenError: method will early return (excute return YES; )  return YES

BUT on return, globalHotKeys still not be released, we have not an opportunity to release the globalHotKeys:  globalHotKeys still not be released

So I wonder that is this a potential memory leak? ( Please see the comment above return YES; )

- (BOOL)isTakenError:(NSError **)outError
{
    CFArrayRef globalHotKeys;
    if (CopySymbolicHotKeys(&globalHotKeys) == noErr) {

        // Enumerate all global hotkeys and check if any of them matches current shortcut
        for (CFIndex i = 0, count = CFArrayGetCount(globalHotKeys); i < count; i++) {
            CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(globalHotKeys, i);
            CFNumberRef code = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
            CFNumberRef flags = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
            CFNumberRef enabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);

            if (([(__bridge NSNumber *)code unsignedIntegerValue] == self.keyCode) &&
                ([(__bridge NSNumber *)flags unsignedIntegerValue] == self.carbonFlags) &&
                ([(__bridge NSNumber *)enabled boolValue])) {

                if (outError) {
                    NSString *description = NSLocalizedString(@"This combination cannot be used because it is already used by a system-wide "
                                                              @"keyboard shortcut.\nIf you really want to use this key combination, most shortcuts "
                                                              @"can be changed in the Keyboard & Mouse panel in System Preferences.",
                                                              @"Message for alert when shortcut is already used by the system");
                    NSDictionary *info = [NSDictionary dictionaryWithObject:description forKey:NSLocalizedDescriptionKey];
                    *outError = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:info];
                }
                /* On return, globalHotKeys still not be released, is this a potential memory leak?
                 * Perhaps there should be a CFRelease( globalHotKeys );
                 */
                return YES;
            }
        }

        CFRelease(globalHotKeys);
    }
    return [self isKeyEquivalent:self.keyCodeStringForKeyEquivalent flags:self.modifierFlags takenInMenu:[NSApp mainMenu] error:outError];
}
shpakovski commented 9 years ago

Yes, indeed! Thanks a ton :) I have fixed this, please verify?

torinkwok commented 9 years ago

No problem now, thank you!