]> khamidou — GitHub mirrors - electron.git/commitdiff
mac: Don't rely on autorelease to delete window delegate
authorCheng Zhao <zcbenz@gmail.com>
Wed, 25 Mar 2015 10:51:29 +0000 (18:51 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 25 Mar 2015 10:51:29 +0000 (18:51 +0800)
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
atom/browser/native_window_mac.mm

index 884ab9899b459eb57be94310c4352235b55d1077..643d885c5dc0fcd6a1f2031cdefb685c27fe0e6e 100644 (file)
@@ -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<NSWindow> window_;
+  base::scoped_nsobject<AtomNSWindow> window_;
+  base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
 
   // The view that will fill the whole frameless window.
   base::scoped_nsobject<FullSizeContentView> content_view_;
index 156ddaa308efb6c6051fa6801a8a5ab6f30f5dd3..6c3a4e268418de2c383ac526b4ed9b4c6e422fdc 100644 (file)
@@ -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<EventProcessingWindow*>(window_);
     DCHECK([event_window isKindOfClass:[EventProcessingWindow class]]);