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

[windows] fix memory leak in MauiWinUIWindow #23327

Merged
merged 2 commits into from
Jul 4, 2024

Conversation

jonathanpeppers
Copy link
Member

Fixes: #22973
Context: https://github.com/danielancines/memory-leak-dotnet8-maui

After reviewing the above sample app, I found that the MauiWinUIWindow class was not ever going away. I could see it being held by NavigationRootManager:

  • Microsoft.Maui.MauiWinUIWindow ->
    • Microsoft.Maui.Platform.NavigationRootManager ->
      • Windows.Foundation.TypedEventHandler<Microsoft.UI.Xaml.Controls.NavigationView, Microsoft.UI.Xaml.Controls.NavigationViewBackRequestedEventArgs>

After some trial and error, I found that simply changing NavigationRootManager's _platformWindow field a WeakReference "untangles" things, and the sample no longer leaks MauiWinUIWindow instances:

image

^^ In this example, I had opened & closed five child windows, so only the currently open window is alive.

Unfortunately, I was not able to write a passing test for this issue. No matter what I tried, the platform Window is kept alive by a "strong handle". But taking a snapshot after the test ends, it goes away?

I still thing the test is useful, as it verifies the other objects.

@jonathanpeppers jonathanpeppers added memory-leak 💦 Memory usage grows / objects live forever platform/windows 🪟 labels Jun 28, 2024
Foda
Foda previously approved these changes Jun 28, 2024
Copy link
Member

@Foda Foda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jonathanpeppers
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Fixes: dotnet#22973
Context: https://github.com/danielancines/memory-leak-dotnet8-maui

After reviewing the above sample app, I found that the
`MauiWinUIWindow` class was not ever going away. I could see it being
held by `NavigationRootManager`:

* `Microsoft.Maui.MauiWinUIWindow` ->
  * `Microsoft.Maui.Platform.NavigationRootManager` ->
    * `Windows.Foundation.TypedEventHandler<Microsoft.UI.Xaml.Controls.NavigationView, Microsoft.UI.Xaml.Controls.NavigationViewBackRequestedEventArgs>`

After some trial and error, I found that simply changing
`NavigationRootManager`'s `_platformWindow` field a `WeakReference`
"untangles" things, and the sample no longer leaks `MauiWinUIWindow`
instances.

Unfortunately, I was not able to write a passing test for this issue.
No matter what I tried, the platform `Window` is kept alive by a
"strong handle". But taking a snapshot after the test ends, it goes
away?

I still thing the test is useful, as it verifies the other objects.
Foda
Foda previously approved these changes Jul 2, 2024
@PureWeen
Copy link
Member

PureWeen commented Jul 2, 2024

/rebase

@jonathanpeppers
Copy link
Member Author

Oh whoops, the new test fails on Android:

System.InvalidOperationException : MauiContext did not have a valid window.

And maybe leaks on iOS:

Reference to Microsoft.Maui.Controls.Window (type Microsoft.Maui.Controls.Window is still alive.

I think last I looked at this CI was down, and so I didn't notice these.

@PureWeen PureWeen merged commit 1628aea into dotnet:main Jul 4, 2024
47 checks passed
@samhouts samhouts added fixed-in-8.0.70 fixed-in-net9.0-nightly This may be available in a nightly release! labels Aug 2, 2024
@samhouts samhouts added fixed-in-9.0.0-preview.7.24407.4 and removed fixed-in-net9.0-nightly This may be available in a nightly release! labels Aug 27, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2024
@jonathanpeppers jonathanpeppers deleted the WindowLeaksOnWindows branch November 1, 2024 15:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Windows] Window Memory Leak
4 participants