From 62f9c3def03c6dcf3b4df530536491db29ff5749 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 25 Mar 2015 18:51:29 +0800 Subject: [PATCH] mac: Don't rely on autorelease to delete window delegate When quiting while closing window, it will leave the window delegate out of the autorelease pool and crash. Fixes #881. --- atom/browser/native_window_mac.h | 5 ++++- atom/browser/native_window_mac.mm | 20 ++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 884ab9899b45..643d885c5dc0 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -14,6 +14,8 @@ #include "base/memory/scoped_ptr.h" #include "atom/browser/native_window.h" +@class AtomNSWindow; +@class AtomNSWindowDelegate; @class FullSizeContentView; class SkRegion; @@ -102,7 +104,8 @@ class NativeWindowMac : public NativeWindow { // whehter we can drag. void InstallDraggableRegionView(); - base::scoped_nsobject window_; + base::scoped_nsobject window_; + base::scoped_nsobject window_delegate_; // 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 156ddaa308ef..6c3a4e268418 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -140,7 +140,6 @@ static const CGFloat kAtomWindowCornerRadius = 4.0; - (void)windowWillClose:(NSNotification*)notification { shell_->NotifyWindowClosed(); - [self autorelease]; } - (BOOL)windowShouldClose:(id)window { @@ -323,21 +322,18 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents, width, height); - AtomNSWindow* atomWindow = [[AtomNSWindow alloc] + window_.reset([[AtomNSWindow alloc] initWithContentRect:cocoa_bounds styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSTexturedBackgroundWindowMask backing:NSBackingStoreBuffered - defer:YES]; + defer:YES]); + [window_ setShell:this]; + [window_ setEnableLargerThanScreen:enable_larger_than_screen_]; - [atomWindow setShell:this]; - [atomWindow setEnableLargerThanScreen:enable_larger_than_screen_]; - window_.reset(atomWindow); - - AtomNSWindowDelegate* delegate = - [[AtomNSWindowDelegate alloc] initWithShell:this]; - [window_ setDelegate:delegate]; + window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]); + [window_ setDelegate:window_delegate_]; if (transparent_) { // Make window has transparent background. @@ -358,7 +354,7 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents, // Enable the NSView to accept first mouse event. bool acceptsFirstMouse = false; options.Get(switches::kAcceptFirstMouse, &acceptsFirstMouse); - [delegate setAcceptsFirstMouse:acceptsFirstMouse]; + [window_delegate_ setAcceptsFirstMouse:acceptsFirstMouse]; // Disable fullscreen button when 'fullscreen' is specified to false. bool fullscreen; @@ -743,7 +739,7 @@ void NativeWindowMac::HandleKeyboardEvent( event.type == content::NativeWebKeyboardEvent::Char) return; - if (event.os_event.window == window_) { + if (event.os_event.window == window_.get()) { EventProcessingWindow* event_window = static_cast(window_); DCHECK([event_window isKindOfClass:[EventProcessingWindow class]]);