Skip to content

Commit

Permalink
macos: break reference cycle to window to allow window to free memory
Browse files Browse the repository at this point in the history
Fixes #366

The comment in the Swift code explains what was happening here:

> I don't know if this is the right place, but because of WindowAccessor in our
> SwiftUI hierarchy, we have a reference cycle between view and window and windows
> are never freed. When the window is closed, the window controller is deinitialized,
> so we can use this opportunity detach the view from the window and break the cycle.

An alternate solution would be to make our reference back to the window
"weak" but we appear to not be able to do that with SwiftUI property
wrappers such as `@State` and `@Binding` and so on.
  • Loading branch information
mitchellh committed Sep 1, 2023
1 parent a6007ca commit d945640
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ class PrimaryWindowController: NSWindowController {
guard let manager = self.windowManager else { return }
manager.triggerNewTab(for: window)
}

deinit {
// I don't know if this is the right place, but because of WindowAccessor in our
// SwiftUI hierarchy, we have a reference cycle between view and window and windows
// are never freed. When the window is closed, the window controller is deinitialized,
// so we can use this opportunity detach the view from the window and break the cycle.
if let window = self.window {
window.contentView = nil
}
}
}

0 comments on commit d945640

Please sign in to comment.