diff --git a/Easydict/objc/EventMonitor/EZEventMonitor.m b/Easydict/objc/EventMonitor/EZEventMonitor.m index d365d3926..c30a385c1 100644 --- a/Easydict/objc/EventMonitor/EZEventMonitor.m +++ b/Easydict/objc/EventMonitor/EZEventMonitor.m @@ -51,6 +51,7 @@ @interface EZEventMonitor () @property (nonatomic, assign) NSInteger currentAlertVolume; @property (nonatomic, assign) CFMachPortRef eventTap; +@property (nonatomic, assign) CFRunLoopSourceRef eventTapRunLoopSource; @property (nonatomic, assign) EZTriggerType frontmostAppTriggerType; @property (nonatomic, assign) BOOL isPopButtonVisible; @@ -235,9 +236,9 @@ - (void)monitorCGEventTap { if (eventTap) { // eventTap must not be NULL, otherwise it will crash. CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); + self.eventTapRunLoopSource = runLoopSource; CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(eventTap, true); - CFRelease(runLoopSource); } } @@ -256,9 +257,13 @@ - (void)stopCGEventTap { // Stop and release the previously created event tap if (self.eventTap) { CGEventTapEnable(self.eventTap, false); // Disable the event tap - CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.eventTap, 0); - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); - CFRelease(runLoopSource); + + if (self.eventTapRunLoopSource) { + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.eventTapRunLoopSource, kCFRunLoopCommonModes); + CFRelease(self.eventTapRunLoopSource); + self.eventTapRunLoopSource = NULL; + } + CFRelease(self.eventTap); self.eventTap = NULL; }