Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macos: break reference cycle to window to allow window to free memory #375

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

mitchellh
Copy link
Contributor

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.

Before

(Open 20 windows, close all)

CleanShot 2023-09-01 at 09 16 17@2x

After

(Open 20 windows, close all)

CleanShot 2023-09-01 at 09 15 17@2x

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.
@mitchellh mitchellh merged commit 4ae1f82 into main Sep 1, 2023
@mitchellh mitchellh deleted the macos-free branch September 1, 2023 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MacOS: closing windows by clicking red X leaks surfaces
1 participant