Use objc's autoreleasepool instead of manually with NSAutoreleasePool (#1936)

Ensures the pools are released even if we panic
This commit is contained in:
Mads Marquart 2021-05-27 17:38:41 +02:00 committed by GitHub
parent 657b4fd59e
commit 982ad46c83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 58 deletions

View file

@ -38,11 +38,12 @@ use cocoa::{
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowStyleMask,
},
base::{id, nil},
foundation::{NSAutoreleasePool, NSDictionary, NSPoint, NSRect, NSSize},
foundation::{NSDictionary, NSPoint, NSRect, NSSize},
};
use core_graphics::display::{CGDisplay, CGDisplayMode};
use objc::{
declare::ClassDecl,
rc::autoreleasepool,
runtime::{Class, Object, Sel, BOOL, NO, YES},
};
@ -118,8 +119,7 @@ fn create_window(
attrs: &WindowAttributes,
pl_attrs: &PlatformSpecificWindowBuilderAttributes,
) -> Option<IdRef> {
unsafe {
let pool = NSAutoreleasePool::new(nil);
autoreleasepool(|| unsafe {
let screen = match attrs.fullscreen {
Some(Fullscreen::Borderless(Some(RootMonitorHandle { inner: ref monitor })))
| Some(Fullscreen::Exclusive(RootVideoMode {
@ -241,9 +241,8 @@ fn create_window(
}
ns_window
});
pool.drain();
res
}
})
}
struct WindowClass(*const Class);
@ -336,17 +335,11 @@ impl UnownedWindow {
}
trace!("Creating new window");
let pool = unsafe { NSAutoreleasePool::new(nil) };
let ns_window = create_window(&win_attribs, &pl_attribs).ok_or_else(|| {
unsafe { pool.drain() };
os_error!(OsError::CreationError("Couldn't create `NSWindow`"))
})?;
let ns_window = create_window(&win_attribs, &pl_attribs)
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSWindow`")))?;
let (ns_view, cursor_state) =
unsafe { create_view(*ns_window, &pl_attribs) }.ok_or_else(|| {
unsafe { pool.drain() };
os_error!(OsError::CreationError("Couldn't create `NSView`"))
})?;
let (ns_view, cursor_state) = unsafe { create_view(*ns_window, &pl_attribs) }
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSView`")))?;
// Configure the new view as the "key view" for the window
unsafe {
@ -422,8 +415,6 @@ impl UnownedWindow {
window.set_maximized(maximized);
}
unsafe { pool.drain() };
Ok((window, delegate))
}