-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jonathanpeppers
added
memory-leak 💦
Memory usage grows / objects live forever
platform/windows 🪟
labels
Jun 28, 2024
Foda
previously approved these changes
Jun 28, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
jonathanpeppers
force-pushed
the
WindowLeaksOnWindows
branch
from
June 28, 2024 18:04
3727870
to
05d0df2
Compare
/azp run |
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.
jonathanpeppers
force-pushed
the
WindowLeaksOnWindows
branch
from
July 2, 2024 14:07
742767d
to
3ea9bf2
Compare
Foda
previously approved these changes
Jul 2, 2024
/rebase |
Oh whoops, the new test fails on Android:
And maybe leaks on iOS:
I think last I looked at this CI was down, and so I didn't notice these. |
PureWeen
approved these changes
Jul 4, 2024
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
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
area-controls-window
Window
fixed-in-8.0.70
fixed-in-9.0.0-preview.7.24407.4
memory-leak 💦
Memory usage grows / objects live forever
platform/windows 🪟
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 byNavigationRootManager
: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 aWeakReference
"untangles" things, and the sample no longer leaksMauiWinUIWindow
instances:^^ 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.