Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[windows] fix VisualDiagnosticsOverlay keeping Page's alive (#23489)
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: 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. I skipped the test on iOS/Catalyst, for now, as I was getting the error: MauiContext should have been set on parent.
- Loading branch information