From b966fc9d5e8a2ff3ea8b96e78757572dc7b5939f Mon Sep 17 00:00:00 2001 From: Dionysis Athinaios Date: Sat, 16 Nov 2024 18:49:43 +0000 Subject: [PATCH] Fix memory leak for Win & Mac --- src/macos/window.rs | 10 +++++++++- src/win/window.rs | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index f945518..8a509a5 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -71,7 +71,6 @@ impl WindowInner { pub(super) fn close(&self) { if self.open.get() { self.open.set(false); - unsafe { // Take back ownership of the NSView's Rc let state_ptr: *const c_void = *(*self.ns_view).get_ivar(BASEVIEW_STATE_IVAR); @@ -87,6 +86,15 @@ impl WindowInner { msg_send![class!(NSNotificationCenter), defaultCenter]; let () = msg_send![notification_center, removeObserver:self.ns_view]; + // Ensure all subviews are detached and released + let subviews: id = msg_send![self.ns_view, subviews]; + let count: usize = msg_send![subviews, count]; + for i in 0..count { + let subview: id = msg_send![subviews, objectAtIndex: i]; + subview.removeFromSuperview(); + let () = msg_send![subview, release]; + } + drop(window_state); // Close the window if in non-parented mode diff --git a/src/win/window.rs b/src/win/window.rs index ac7824b..0d937fc 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -76,6 +76,7 @@ impl WindowHandle { if let Some(hwnd) = self.hwnd.take() { unsafe { PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0); + DestroyWindow(hwnd); } } }