Skip to content

Commit

Permalink
Fix memory leak for Win & Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
dathinaios committed Nov 16, 2024
1 parent cd4df61 commit b966fc9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WindowState>
let state_ptr: *const c_void = *(*self.ns_view).get_ivar(BASEVIEW_STATE_IVAR);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl WindowHandle {
if let Some(hwnd) = self.hwnd.take() {
unsafe {
PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0);
DestroyWindow(hwnd);
}
}
}
Expand Down

0 comments on commit b966fc9

Please sign in to comment.