From e96e674201c7dfccf7f39f8b22711f3a803de7e9 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Thu, 21 Jan 2016 16:31:31 -0800 Subject: [PATCH] Properly clean up NSEvent monitor --- atom/browser/native_window_mac.h | 1 + atom/browser/native_window_mac.mm | 33 +++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index ef3ccb1502..927e971d9b 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -110,6 +110,7 @@ class NativeWindowMac : public NativeWindow { base::scoped_nsobject window_; base::scoped_nsobject window_delegate_; + base::scoped_nsobject event_monitor_; // The view that will fill the whole frameless window. base::scoped_nsobject content_view_; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 198a78e3f5..88476eafbf 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -491,28 +491,31 @@ NativeWindowMac::NativeWindowMac( [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; BOOL __block down = NO; - [NSEvent addLocalMonitorForEventsMatchingMask:NSScrollWheelMask handler:^NSEvent * _Nullable(NSEvent * event) { - if (![window_ isKeyWindow]) - return event; + event_monitor_.reset([[NSEvent + addLocalMonitorForEventsMatchingMask:NSScrollWheelMask + handler:^NSEvent * _Nullable(NSEvent * event) { + if (![window_ isKeyWindow]) + return event; - if (!web_contents) - return event; + if (!web_contents) + return event; - if (!down && (([event phase] == NSEventPhaseMayBegin) || ([event phase] == NSEventPhaseBegan))) { - this->NotifyWindowScrollTouchDown(); - down = YES; - } - if (down && (([event phase] == NSEventPhaseEnded) || ([event phase] == NSEventPhaseCancelled))) { - this->NotifyWindowScrollTouchUp(); - down = NO; - } - return event; - }]; + if (!down && (([event phase] == NSEventPhaseMayBegin) || ([event phase] == NSEventPhaseBegan))) { + this->NotifyWindowScrollTouchBegin(); + down = YES; + } + if (down && (([event phase] == NSEventPhaseEnded) || ([event phase] == NSEventPhaseCancelled))) { + this->NotifyWindowScrollTouchEnd(); + down = NO; + } + return event; + }] retain]); InstallView(); } NativeWindowMac::~NativeWindowMac() { + [NSEvent removeMonitor: event_monitor_.get()]; Observe(nullptr); }