-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix memory leak in FlyoutPage when changing the Detail #33358
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
c448ecc
832c180
65cf2c4
6ac1a37
989ca43
975eb88
1e77dfb
c935d8c
9a351d5
3ccd638
5ca66d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ public Page Detail | |
| { | ||
| previousDetail.SendNavigatedFrom( | ||
| new NavigatedFromEventArgs(destinationPage: value, NavigationType.Replace)); | ||
| previousDetail.Handler?.DisconnectHandler(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine but we would want to call DisconnectHAndler after the page is unloaded if you look at "void OnPageChanged(Page? oldPage, Page? newPage)" inside window you'll see where I wire into the unloaded event and then call disocnnecthandler from there.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will not work, that method isn't called when the |
||
| } | ||
|
|
||
| _detail.SendNavigatedTo(new NavigatedToEventArgs(previousDetail, NavigationType.Replace)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,6 +85,7 @@ void SetupBuilder() | |||||||||||||
| handlers.AddHandler<TimePicker, TimePickerHandler>(); | ||||||||||||||
| handlers.AddHandler<Toolbar, ToolbarHandler>(); | ||||||||||||||
| handlers.AddHandler<WebView, WebViewHandler>(); | ||||||||||||||
| handlers.AddHandler<FlyoutPage, FlyoutViewHandler>(); | ||||||||||||||
|
|
||||||||||||||
| #if IOS || MACCATALYST | ||||||||||||||
| handlers.AddHandler<NavigationPage, NavigationRenderer>(); | ||||||||||||||
|
|
@@ -577,6 +578,90 @@ await CreateHandlerAndAddToWindow(window, async () => | |||||||||||||
| await AssertionExtensions.WaitForGC([.. references]); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| [Fact("FlyoutPage Detail Does Not Leak When Replaced")] | ||||||||||||||
| public async Task FlyoutPageDetailDoesNotLeak() | ||||||||||||||
| { | ||||||||||||||
| SetupBuilder(); | ||||||||||||||
|
|
||||||||||||||
| var references = new List<WeakReference>(); | ||||||||||||||
| var flyoutPage = new FlyoutPage | ||||||||||||||
| { | ||||||||||||||
| Flyout = new ContentPage { Title = "Flyout" }, | ||||||||||||||
| Detail = new ContentPage { Title = "Detail" } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| await CreateHandlerAndAddToWindow(new Window(flyoutPage), async () => | ||||||||||||||
| { | ||||||||||||||
| await OnLoadedAsync(flyoutPage); | ||||||||||||||
|
|
||||||||||||||
| var detailPage1 = new ContentPage { Title = "Detail 1" }; | ||||||||||||||
| var navPage1 = new NavigationPage(detailPage1); | ||||||||||||||
| flyoutPage.Detail = navPage1; | ||||||||||||||
|
|
||||||||||||||
| await OnLoadedAsync(detailPage1); | ||||||||||||||
|
|
||||||||||||||
| references.Add(new(navPage1)); | ||||||||||||||
| references.Add(new(navPage1.Handler)); | ||||||||||||||
| references.Add(new(navPage1.Handler.PlatformView)); | ||||||||||||||
| references.Add(new(detailPage1)); | ||||||||||||||
| references.Add(new(detailPage1.Handler)); | ||||||||||||||
| references.Add(new(detailPage1.Handler.PlatformView)); | ||||||||||||||
|
|
||||||||||||||
| var detailPage2 = new ContentPage { Title = "Detail 2" }; | ||||||||||||||
| var navPage2 = new NavigationPage(detailPage2); | ||||||||||||||
| flyoutPage.Detail = navPage2; | ||||||||||||||
|
|
||||||||||||||
| await OnLoadedAsync(detailPage2); | ||||||||||||||
|
|
||||||||||||||
| navPage1 = null; | ||||||||||||||
| detailPage1 = null; | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| await AssertionExtensions.WaitForGC([.. references]); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| [Fact("FlyoutPage Detail Does Not Leak With Multiple Replacements")] | ||||||||||||||
| public async Task FlyoutPageDetailDoesNotLeakWithMultipleReplacements() | ||||||||||||||
| { | ||||||||||||||
| SetupBuilder(); | ||||||||||||||
|
|
||||||||||||||
| var references = new List<WeakReference>(); | ||||||||||||||
| var flyoutPage = new FlyoutPage | ||||||||||||||
| { | ||||||||||||||
| Flyout = new ContentPage { Title = "Flyout" }, | ||||||||||||||
| Detail = new ContentPage { Title = "Detail" } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| await CreateHandlerAndAddToWindow(new Window(flyoutPage), async () => | ||||||||||||||
| { | ||||||||||||||
| await OnLoadedAsync(flyoutPage); | ||||||||||||||
|
|
||||||||||||||
| for (int i = 0; i < 5; i++) | ||||||||||||||
| { | ||||||||||||||
| var detailPage = new ContentPage { Title = $"Detail {i}" }; | ||||||||||||||
| var navPage = new NavigationPage(detailPage); | ||||||||||||||
|
|
||||||||||||||
| flyoutPage.Detail = navPage; | ||||||||||||||
| await OnLoadedAsync(detailPage); | ||||||||||||||
|
|
||||||||||||||
| if (i < 3) | ||||||||||||||
| { | ||||||||||||||
| references.Add(new(navPage)); | ||||||||||||||
| references.Add(new(navPage.Handler)); | ||||||||||||||
| references.Add(new(navPage.Handler.PlatformView)); | ||||||||||||||
| references.Add(new(detailPage)); | ||||||||||||||
| references.Add(new(detailPage.Handler)); | ||||||||||||||
| references.Add(new(detailPage.Handler.PlatformView)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| // Give the platform time to complete disposal/teardown of the previous Detail | |
| // (handlers, native views, fragments) before proceeding to the next iteration. | |
| // This small delay has been found necessary to make the subsequent GC-based | |
| // memory assertions reliable across devices. |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -18,5 +18,11 @@ public MauiNavHostFragment() | |||
| protected MauiNavHostFragment(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) | ||||
| { | ||||
| } | ||||
|
|
||||
| public override void OnDestroy() | ||||
| { | ||||
| base.OnDestroy(); | ||||
| this.Dispose(); | ||||
|
||||
| this.Dispose(); |
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.
not calling the dispose causes the memory leak
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.
Hmmm this seems wrong though, we shouldn't need to call Dispose here to fix a memory leak.
This seems like maybe the dispose is triggering something else that's possibilty fixing the leak?
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.
@PureWeen not sure... Maybe it's somehow removing it from the fragmentManager? The leaks on android specific code was related to bad clean up on FragmentManager, before trying to call dispose I did all I find to remove the navHost from the fragment manager but nothing helped. Maybe calling the dispose from StackNavigationManager would be better?
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -89,8 +89,10 @@ public override void OnDestroy() | |||
| { | ||||
| _currentView = null; | ||||
| _fragmentContainerView = null; | ||||
| _navigationManager = null; | ||||
|
|
||||
| base.OnDestroy(); | ||||
| this.Dispose(); | ||||
|
||||
| this.Dispose(); |
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.
not calling the dispose causes the memory leak
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,8 @@ public override void OnDestroy() | |||||||||||||||||
| { | ||||||||||||||||||
| base.OnDestroy(); | ||||||||||||||||||
| IsDestroyed = true; | ||||||||||||||||||
|
|
||||||||||||||||||
| DetailView = null!; | ||||||||||||||||||
|
Comment on lines
37
to
+40
|
||||||||||||||||||
| base.OnDestroy(); | |
| IsDestroyed = true; | |
| DetailView = null!; | |
| IsDestroyed = true; | |
| DetailView = null!; | |
| base.OnDestroy(); |
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.
Not 100% sure about this one, maybe the dev. should be responsible to call the
DisconnectHandler?