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

Remove implicit retain of self in eventMonitor block #119

Closed danmessing closed 6 years ago

danmessing commented 6 years ago

In MASShortcutView.m, -activateEventMonitoring: was implicitly retaining self in two places by using the _shortcutValidator variable. In certain cases, this caused the control to fail to recognize new keystrokes.

To see the problem in action, run the attached sample project. Click shortcut view, then press command-w to close the window (before a shortcut has been set). Open a new document window and click the shortcut view there - at this point modifier keys will not register in the view.

The reason seems to be that because self is retained buy the block, when the new control calls -activateEventMonitoring:, the old block and therefore the old self is dealloc'ed, which causes activateEventMonitoring: to be called a second time with NO.

TestApp.zip

shpakovski commented 6 years ago

Perfect, thanks @danmessing!