fix: memory leak in macOS MetalLayer management

When using WebGPU front end, creating and closing windows will cause
VM:IOSurface memory leak, due to an unreleased object reference.

Also with some other trivial memory leak prevention measures.
This commit is contained in:
I-Info authored and Wez Furlong committed 2025-10-14 19:33:09 +01:00
1 parent bb297506db
commit c8a649684d
2 files changed
+18 -7

No files matched your search

+11 -1
View File
@@ -1492,13 +1492,20 @@ impl TermWindow {
return mux.get_window(mux_window_id).is_some();
}
MuxNotification::TabAddedToWindow { window_id, .. }
| MuxNotification::WindowRemoved(window_id)
| MuxNotification::WindowTitleChanged { window_id, .. }
| MuxNotification::WindowInvalidated(window_id) => {
if window_id != mux_window_id {
return true;
}
}
MuxNotification::WindowRemoved(window_id) => {
if window_id != mux_window_id {
return true;
}
// Set the window as dead to unsubscribe from further notifications
dead.store(true, Ordering::Relaxed);
return false;
}
MuxNotification::TabResized(tab_id)
| MuxNotification::TabTitleChanged { tab_id, .. } => {
let mux = Mux::get();
@@ -1538,6 +1545,9 @@ impl TermWindow {
let mux = Mux::get();
let dead = Arc::new(AtomicBool::new(false));
mux.subscribe(move |n| {
if dead.load(Ordering::Relaxed) {
return false;
}
let mux_window_id = *mux_window_id.lock().unwrap();
let window = window.clone();
let dead = dead.clone();
+7 -6
View File
@@ -586,8 +586,7 @@ impl Window {
window.setTitle_(*nsstring(&name));
window.setAcceptsMouseMovedEvents_(YES);
let view = WindowView::alloc(&inner)?;
view.initWithFrame_(rect);
let view = WindowView::init_with_frame(&inner, rect)?;
view.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable);
let () = msg_send![
@@ -3035,7 +3034,9 @@ impl WindowView {
log::trace!("make_backing_layer");
let class = class!(CAMetalLayer);
unsafe {
let layer: id = msg_send![class, new];
// Use type method to get a instance of CAMetalLayer.
// So that we don't have to worry about retaining/releasing it.
let layer: id = msg_send![class, layer];
let () = msg_send![layer, setDelegate: view];
let () = msg_send![layer, setContentsScale: 1.0];
let () = msg_send![layer, setOpaque: NO];
@@ -3152,11 +3153,11 @@ impl WindowView {
}
}
fn alloc(inner: &Rc<RefCell<Inner>>) -> anyhow::Result<StrongPtr> {
fn init_with_frame(inner: &Rc<RefCell<Inner>>, rect: NSRect) -> anyhow::Result<StrongPtr> {
let cls = Self::get_class();
let view_id: StrongPtr = unsafe { StrongPtr::new(msg_send![cls, new]) };
let view_id: id = unsafe { msg_send![cls, alloc] };
let view_id: StrongPtr = unsafe { StrongPtr::new(msg_send![view_id, initWithFrame:rect]) };
inner.borrow_mut().view_id.replace(view_id.weak());
let view = Box::into_raw(Box::new(Self {