From c8a649684d6d2ea3c0f9767be3979a82a1b018df Mon Sep 17 00:00:00 2001 From: I-Info Date: Mon, 6 Oct 2025 22:23:47 +1100 Subject: [PATCH] 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. --- wezterm-gui/src/termwindow/mod.rs | 12 +++++++++++- window/src/os/macos/window.rs | 13 +++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index ae665b970..350cb8fb7 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -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(); diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 9f6db3ade..6d71a7106 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -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>) -> anyhow::Result { + fn init_with_frame(inner: &Rc>, rect: NSRect) -> anyhow::Result { 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 {