-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macos multi windows leak #481
Changes from 7 commits
6cfb845
1d6d739
ab4e7a8
6ea3736
dc02950
69096b6
ebe1312
b11259f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ use cocoa; | |
use cocoa::appkit::{self, NSApplication, NSColor, NSScreen, NSView, NSWindow, NSWindowButton, | ||
NSWindowStyleMask}; | ||
use cocoa::base::{id, nil}; | ||
use cocoa::foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSString}; | ||
use cocoa::foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSString, NSAutoreleasePool}; | ||
|
||
use core_graphics::display::CGDisplay; | ||
|
||
|
@@ -452,9 +452,15 @@ impl WindowDelegate { | |
unsafe { | ||
let delegate = IdRef::new(msg_send![WindowDelegate::class(), new]); | ||
|
||
// setDelegate uses autorelease on objects, | ||
// so need autorelease | ||
let autoreleasepool = NSAutoreleasePool::new(cocoa::base::nil); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason you went with |
||
|
||
(&mut **delegate).set_ivar("winitState", state_ptr as *mut ::std::os::raw::c_void); | ||
let _: () = msg_send![*state.window, setDelegate:*delegate]; | ||
|
||
let _: () = msg_send![autoreleasepool, drain]; | ||
|
||
WindowDelegate { state: state, _this: delegate } | ||
} | ||
} | ||
|
@@ -464,7 +470,11 @@ impl Drop for WindowDelegate { | |
fn drop(&mut self) { | ||
unsafe { | ||
// Nil the window's delegate so it doesn't still reference us | ||
// NOTE: setDelegate:nil at first retains the previous value, | ||
// and then autoreleases it, so autorelease pool is needed | ||
let autoreleasepool = NSAutoreleasePool::new(cocoa::base::nil); | ||
let _: () = msg_send![*self.state.window, setDelegate:nil]; | ||
let _: () = msg_send![autoreleasepool, drain]; | ||
} | ||
} | ||
} | ||
|
@@ -505,13 +515,33 @@ unsafe fn get_current_monitor() -> RootMonitorId { | |
|
||
impl Drop for Window2 { | ||
fn drop(&mut self) { | ||
// Remove this window from the `EventLoop`s list of windows. | ||
// The destructor order is: | ||
// Window -> | ||
// Rc<Window2> (makes Weak<..> in shared.windows none) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also be nice to have |
||
// Window2 | ||
// needed to remove the element from array | ||
let id = self.id(); | ||
if let Some(shared) = self.delegate.state.shared.upgrade() { | ||
shared.find_and_remove_window(id); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra newline |
||
// nswindow::close uses autorelease | ||
// so autorelease pool | ||
let autoreleasepool = unsafe { | ||
NSAutoreleasePool::new(cocoa::base::nil) | ||
}; | ||
|
||
// Close the window if it has not yet been closed. | ||
let nswindow = *self.window; | ||
if nswindow != nil { | ||
unsafe { | ||
let () = msg_send![nswindow, close]; | ||
} | ||
} | ||
|
||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
} | ||
} | ||
|
||
|
@@ -538,20 +568,29 @@ impl Window2 { | |
panic!("Windows can only be created on the main thread on macOS"); | ||
} | ||
} | ||
let autoreleasepool = unsafe { | ||
NSAutoreleasePool::new(cocoa::base::nil) | ||
}; | ||
|
||
let app = match Window2::create_app(pl_attribs.activation_policy) { | ||
Some(app) => app, | ||
None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, | ||
None => { | ||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
return Err(OsError(format!("Couldn't create NSApplication"))); }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}; | ||
|
||
let window = match Window2::create_window(win_attribs, pl_attribs) | ||
{ | ||
Some(window) => window, | ||
None => { return Err(OsError(format!("Couldn't create NSWindow"))); }, | ||
None => { | ||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
return Err(OsError(format!("Couldn't create NSWindow"))); }, | ||
}; | ||
let view = match Window2::create_view(*window) { | ||
Some(view) => view, | ||
None => { return Err(OsError(format!("Couldn't create NSView"))); }, | ||
None => { | ||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
return Err(OsError(format!("Couldn't create NSView"))); }, | ||
}; | ||
|
||
unsafe { | ||
|
@@ -618,6 +657,8 @@ impl Window2 { | |
window.delegate.state.perform_maximized(win_attribs.maximized); | ||
} | ||
|
||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
|
||
Ok(window) | ||
} | ||
|
||
|
@@ -638,11 +679,30 @@ impl Window2 { | |
} | ||
} | ||
|
||
fn class() -> *const Class { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra newline |
||
static mut WINDOW2_CLASS: *const Class = 0 as *const Class; | ||
static INIT: std::sync::Once = std::sync::ONCE_INIT; | ||
|
||
INIT.call_once(|| unsafe { | ||
let window_superclass = Class::get("NSWindow").unwrap(); | ||
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap(); | ||
decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL); | ||
decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL); | ||
WINDOW2_CLASS = decl.register(); | ||
}); | ||
|
||
unsafe { | ||
WINDOW2_CLASS | ||
} | ||
} | ||
|
||
fn create_window( | ||
attrs: &WindowAttributes, | ||
pl_attrs: &PlatformSpecificWindowBuilderAttributes) | ||
-> Option<IdRef> { | ||
unsafe { | ||
let autoreleasepool = NSAutoreleasePool::new(cocoa::base::nil); | ||
let screen = match attrs.fullscreen { | ||
Some(ref monitor_id) => { | ||
let monitor_screen = monitor_id.inner.get_nsscreen(); | ||
|
@@ -685,14 +745,7 @@ impl Window2 { | |
NSWindowStyleMask::NSTitledWindowMask | ||
}; | ||
|
||
let winit_window = Class::get("WinitWindow").unwrap_or_else(|| { | ||
let window_superclass = Class::get("NSWindow").unwrap(); | ||
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap(); | ||
decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL); | ||
decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL); | ||
decl.register(); | ||
Class::get("WinitWindow").unwrap() | ||
}); | ||
let winit_window = Window2::class(); | ||
|
||
let window: id = msg_send![winit_window, alloc]; | ||
|
||
|
@@ -702,7 +755,7 @@ impl Window2 { | |
appkit::NSBackingStoreBuffered, | ||
NO, | ||
)); | ||
window.non_nil().map(|window| { | ||
let res = window.non_nil().map(|window| { | ||
let title = IdRef::new(NSString::alloc(nil).init_str(&attrs.title)); | ||
window.setReleasedWhenClosed_(NO); | ||
window.setTitle_(*title); | ||
|
@@ -735,7 +788,9 @@ impl Window2 { | |
|
||
window.center(); | ||
window | ||
}) | ||
}); | ||
let _: () = msg_send![autoreleasepool, drain]; | ||
res | ||
} | ||
} | ||
|
||
|
@@ -1109,7 +1164,12 @@ impl IdRef { | |
impl Drop for IdRef { | ||
fn drop(&mut self) { | ||
if self.0 != nil { | ||
let _: () = unsafe { msg_send![self.0, release] }; | ||
unsafe { | ||
let autoreleasepool = | ||
NSAutoreleasePool::new(cocoa::base::nil); | ||
let _ : () = msg_send![self.0, release]; | ||
let _ : () = msg_send![autoreleasepool, release]; | ||
}; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont need to keep dead week ptrs here