[windows] fix VisualDiagnosticsOverlay keeping Page's alive#23489
Merged
jonathanpeppers merged 2 commits intoJul 10, 2024
Conversation
Fixes: dotnet#23034 Context: https://github.com/user-attachments/files/15817814/MauiApp1.zip In the above sample app, you can observe a memory leak on Windows by doing: Application.Current.MainPage = new Page1(); // This page lives forever Application.Current.MainPage = new Page2(); Reviewing memory snapshots, it appears that the `VisualDiagnosticsOverlay` class is keeping the `Page1` instance alive: MauiApp1.Page1 -> Microsoft.Maui.Platform.ContentPanel -> Microsoft.Maui.Controls.VisualDiagnostics.VisualDiagnosticsOverlay -> Microsoft.Maui.Controls.Window In this case, the `Window` holds a reference to the `VisualDiagnosticsOverlay`, and the `Window` is still open. `VisualDiagnosticsOverlay`'s base class is `WindowOverlay`, which has a `_platformElement` field holding the `ContentPanel` which holds the `Page1` instance. In this case, `_platformElement` is only used for unsubscribing events, so it feels like we can just change it to a `WeakReference` to solve the issue. I was able to write a test for this scenario as well.
Member
Author
|
The iOS/Catalyst tests have a failure: |
jonathanpeppers
commented
Jul 9, 2024
Comment on lines
+438
to
+442
| [Fact("VisualDiagnosticsOverlay Does Not Leak" | ||
| #if IOS || MACCATALYST | ||
| , Skip = "Fails with 'MauiContext should have been set on parent.'" | ||
| #endif | ||
| )] |
Member
Author
There was a problem hiding this comment.
I did not figure out how to get this test to work on iOS/Catalyst. It seems like it wants the Window's Handler to be created for it to work.
In either case, I think there is only a problem on Windows here.
Redth
approved these changes
Jul 10, 2024
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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: #23034
Context: https://github.com/user-attachments/files/15817814/MauiApp1.zip
In the above sample app, you can observe a memory leak on Windows by doing:
Reviewing memory snapshots, it appears that the
VisualDiagnosticsOverlayclass is keeping thePage1instance alive:In this case, the
Windowholds a reference to theVisualDiagnosticsOverlay, and theWindowis still open.VisualDiagnosticsOverlay's base class isWindowOverlay, which has a_platformElementfield holding theContentPanelwhich holds thePage1instance.In this case,
_platformElementis only used for unsubscribing events, so it feels like we can just change it to aWeakReferenceto solve the issue.I was able to write a test for this scenario as well.