fix(Android): null Screen.fragmentWrapper on dismiss to fix Fabric retain cycle#3855
Open
ghenry22 wants to merge 1 commit intosoftware-mansion:mainfrom
Open
fix(Android): null Screen.fragmentWrapper on dismiss to fix Fabric retain cycle#3855ghenry22 wants to merge 1 commit intosoftware-mansion:mainfrom
ghenry22 wants to merge 1 commit intosoftware-mansion:mainfrom
Conversation
Fabric's SurfaceMountingManager.mTagToViewState keeps Screen views alive for the lifetime of the surface. Because Screen.fragmentWrapper still points to the destroyed ScreenFragment after dismissal, the fragment and everything it transitively retains can never be GC'd. ScreenFragment.onDestroy now nulls the back-reference, gated on the existing isScreenDismissed discriminator so it only fires when the screen has actually been removed from its container. ScreenViewManager .onDropViewInstance does the same as a belt-and-braces fallback for the case where Fabric drops the view without onDestroy firing on schedule, guarded so it only nulls when no fragment is currently attached. See: software-mansion#3755
Member
|
Thanks for the report. We recently did an investigation & AFAIK there should not be a leak - we'll investigate this nonetheless. cc @t0maboro can I ask you to take a look here? |
Contributor
|
EDIT: #3867 - noticed another issue with headerShown: true/false |
Contributor
|
Also, @ghenry22 would you be able to prepare and add to this PR a new example that shows the difference between main and this PR - it'd be really helpful for regression testing in the future |
4 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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Closes #3755.
On Fabric,
SurfaceMountingManager.mTagToViewStatekeepsScreenviews alive for the lifetime of the surface. BecauseScreen.fragmentWrapperstill points to the destroyedScreenFragmentafter dismissal, the fragment — and everything it transitively retains — can never be GC'd. The reporter on #3755 measured ~1.2 MB per back-nav cycle on a Galaxy A7 / API 29 with rnscreens 4.20–4.24.The fix nulls
Screen.fragmentWrapperinScreenFragment.onDestroy, gated on the existingisScreenDismisseddiscriminator thatonDestroyalready uses to decide whether to dispatchScreenDismissedEvent:This gating matters because
onDestroycan fire in lifecycle paths where the screen has not actually been removed from its container. The unconditional null proposed in #3755 crashes in those paths withIllegalStateException: Parent Screen does not have its Fragment attachedatScreenContainer.setupFragmentManager. Gating on the existinghasScreen()check leaves the back-reference intact in non-dismissal paths and only breaks the cycle when the screen is actually gone.A second hunk in
ScreenViewManager.onDropViewInstancedoes the same cleanup as a fallback for the case where Fabric drops the view withoutonDestroyfiring on schedule, guarded byview.fragmentWrapper?.fragment?.isAdded != trueso it only nulls when no live fragment is attached.Library precedent for both pieces:
ScreenContainer.parentScreenWrapperis already nulled inonDetachedFromWindow().ScreenStackHeaderConfigViewManager.onDropViewInstancealready callsview.destroy().Changes
ScreenFragment.kt— hoist the existing dismissal check into a namedisScreenDismissedlocal, then nullscreen.fragmentWrapperinside that branch with an=== thisidentity guard.ScreenViewManager.kt— add anonDropViewInstance(view: Screen)override that nullsview.fragmentWrapperwhen no fragment is currently attached, then callssuper.Test plan
Validation done:
patch-packagepatch.Reproducer for the leak (from #3755):
ScreenFragmentinstances appear with the chainScreen.fragmentWrapper → ScreenFragment(andScreenContainer.parentScreenWrapper → ScreenStackFragmentfor stack fragments). The reporter on [Android][Fabric] Screen.fragmentWrapper retains destroyed ScreenFragment via Fabric's mTagToViewState #3755 measured ~1.2 MB per cycle on a Galaxy A7 / API 29.Screen.fragmentWrapperand the fragments are GC'd on the next collection.Failure mode this fix avoids:
The unconditional
screen.fragmentWrapper = nullproposed in #3755 produces:…on back-nav from any pushed screen, because
onDestroyfires for screens whose wrapper is still needed by the in-flight transaction. The discriminator-gating in this PR avoids that path entirely — thehasScreen()check returnstruefor those screens, and we don't touch the wrapper.Checklist