[android] fix memory leak with CarouselView#18584
Merged
rmarinho merged 1 commit intodotnet:mainfrom Nov 8, 2023
Merged
Conversation
Fixes: dotnet#17726 Adding `CarouselView` to a page and navigating away from it keeps the page alive forever on Android. I was able to reproduce this in a test. It took me a while to actually track this one down, but the problem boils down to `MauiCarouselRecyclerView` doing: _carouselViewLayoutListener = new CarouselViewwOnGlobalLayoutListener(); _carouselViewLayoutListener.LayoutReady += LayoutReady; ViewTreeObserver.AddOnGlobalLayoutListener(_carouselViewLayoutListener); The `ViewTreeObserver` lives longer than the page, and so: * `ViewTreeObserver` -> `CarouselViewwOnGlobalLayoutListener` * `event LayoutReady` -> `MauiCarouselRecyclerView` * `MauiCarouselRecyclerView` -> `CarouselView` * `CarouselView` -> `Parent.Parent.Parent.*` -> `Page` Thus the `Page` lives forever. If we remove the `LayoutReady` event, instead: * `CarouselViewwOnGlobalLayoutListener` saves `MauiCarouselRecyclerView` in a weak reference. * It can just call `LayoutReady()` directly. * `MauiCarouselRecyclerView` is able to be GC'd now. * `MauiCarouselRecyclerView.Dispose()` already appropriately will call `ClearLayoutListener()` * Lastly, `ViewTreeObserver?.RemoveOnGlobalLayoutListener(_carouselViewLayoutListener)` With these changes, the test passes and I don't think anything will leak now. :) Other changes: * I fixed the typo in `CarouselViewwOnGlobalLayoutListener`. * I sorted the type names in the test alphabetically.
jonathanpeppers
commented
Nov 7, 2023
| { | ||
| public EventHandler<EventArgs> LayoutReady; | ||
| public void OnGlobalLayout() | ||
| class CarouselViewOnGlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener |
Member
Author
There was a problem hiding this comment.
I made this a nested private class, so it could access the LayoutReady private method.
rmarinho
approved these changes
Nov 8, 2023
21 tasks
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: #17726
Related: #18365
Adding
CarouselViewto a page and navigating away from it keeps the page alive forever on Android. I was able to reproduce this in a test.It took me a while to actually track this one down, but the problem boils down to
MauiCarouselRecyclerViewdoing:The
ViewTreeObserverlives longer than the page, and so:ViewTreeObserver->CarouselViewwOnGlobalLayoutListenerevent LayoutReady->MauiCarouselRecyclerViewMauiCarouselRecyclerView->CarouselViewCarouselView->Parent.Parent.Parent.*->PageThus the
Pagelives forever.If we remove the
LayoutReadyevent, instead:CarouselViewwOnGlobalLayoutListenersavesMauiCarouselRecyclerViewin a weak reference.It can just call
LayoutReady()directly.MauiCarouselRecyclerViewis able to be GC'd now.MauiCarouselRecyclerView.Dispose()already appropriately will callClearLayoutListener()Lastly,
ViewTreeObserver?.RemoveOnGlobalLayoutListener(_carouselViewLayoutListener)With these changes, the test passes and I don't think anything will leak now. :)
Other changes:
CarouselViewwOnGlobalLayoutListener.