From b966fc9d5e8a2ff3ea8b96e78757572dc7b5939f Mon Sep 17 00:00:00 2001 From: Dionysis Athinaios Date: Sat, 16 Nov 2024 18:49:43 +0000 Subject: [PATCH 1/4] 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); } } } From ef233361ef6331bc67f17bf7fed59a4ce20231ab Mon Sep 17 00:00:00 2001 From: Dionysis Athinaios Date: Sun, 17 Nov 2024 10:48:52 +0000 Subject: [PATCH 2/4] Remove retain call instead on Mac --- src/gl/macos.rs | 1 - src/macos/window.rs | 9 --------- 2 files changed, 10 deletions(-) diff --git a/src/gl/macos.rs b/src/gl/macos.rs index 05f2aa2..323a40d 100644 --- a/src/gl/macos.rs +++ b/src/gl/macos.rs @@ -91,7 +91,6 @@ impl GlContext { view.setWantsBestResolutionOpenGLSurface_(YES); - let () = msg_send![view, retain]; NSOpenGLView::display_(view); parent_view.addSubview_(view); diff --git a/src/macos/window.rs b/src/macos/window.rs index 8a509a5..6bdd2be 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -86,15 +86,6 @@ 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 From 484eb90f77c0690269ca8086fd8b74f1bd014a05 Mon Sep 17 00:00:00 2001 From: Dionysis Athinaios Date: Sun, 17 Nov 2024 20:13:17 +0000 Subject: [PATCH 3/4] Comment out PostMessage --- src/win/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win/window.rs b/src/win/window.rs index 0d937fc..47976ee 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -75,7 +75,7 @@ impl WindowHandle { pub fn close(&mut self) { if let Some(hwnd) = self.hwnd.take() { unsafe { - PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0); + // PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0); DestroyWindow(hwnd); } } From 960b48a749dca766d423de9d99685b660f73cfaf Mon Sep 17 00:00:00 2001 From: Dionysis Athinaios Date: Mon, 18 Nov 2024 07:31:07 +0000 Subject: [PATCH 4/4] Reverse Windows changes --- src/win/window.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 47976ee..ac7824b 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -75,8 +75,7 @@ impl WindowHandle { pub fn close(&mut self) { if let Some(hwnd) = self.hwnd.take() { unsafe { - // PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0); - DestroyWindow(hwnd); + PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0); } } }