Skip to content
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

View.backPressHandler memory leak fix. #894

Merged
merged 2 commits into from
Oct 31, 2022
Merged

View.backPressHandler memory leak fix. #894

merged 2 commits into from
Oct 31, 2022

Conversation

rjrjr
Copy link
Contributor

@rjrjr rjrjr commented Oct 28, 2022

We now strictly require that a ViewTreeLifecycleOwner can be found so that we can be certain of getting a callback to remove the OnBackPressedCallback from the OnBackPressedDispatcher. We also null out the guts of the callback while the View is detached, to be doubly sure not to leak anything even if the ViewTreeLifecycleOwner is misbehaving.

Fixes #889.

We now strictly require that a `ViewTreeLifecycleOwner` can be found so that we can be certain of getting a callback to remove the `OnBackPressedCallback` from the `OnBackPressedDispatcher`. We also null out the guts of the callback while the `View` is detached, to be doubly sure not to leak anything even if the `ViewTreeLifecycleOwner` is misbehaving.

Fixes #889.
@rjrjr rjrjr force-pushed the ray/leaky-backhandler branch from 90d4cd3 to f56b3ce Compare October 28, 2022 22:07
@rjrjr rjrjr marked this pull request as ready for review October 28, 2022 22:08
@rjrjr rjrjr requested review from a team and zach-klippenstein as code owners October 28, 2022 22:08
@rjrjr rjrjr force-pushed the ray/leaky-backhandler branch 3 times, most recently from ef8d100 to b8ac7ba Compare October 28, 2022 23:34
@rjrjr rjrjr force-pushed the ray/leaky-backhandler branch from b8ac7ba to 16de577 Compare October 28, 2022 23:57
}

public ArrayDeque<OnBackPressedCallback> callbacks() {
return dispatcher.mOnBackPressedCallbacks;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Collection or List maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm exposing the implementation of an Android class for testing purposes, I don't think there's a real need to abstract it.

Comment on lines +106 to +109
lifecycleOrNull?.let { lifecycle ->
lifecycle.removeObserver(this)
lifecycleOrNull = null
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

lifecycleOrNull?.removeObserver(this)
lifecycleOrNull = null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact you can do

lifecycleOrNull?.removeObserver(this)
lifecycleOrNull = ViewTreeLifecycleOwner.get(view)?.lifecycle

Comment on lines -115 to -117
/**
* Weak reference ensures that we don't leak the view.
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL

Copy link

@helios175 helios175 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly sure how this fixes any leak, but looks correct.

techincally-correct

@rjrjr
Copy link
Contributor Author

rjrjr commented Oct 31, 2022

Not exactly sure how this fixes any leak, but looks correct.

The old code would register with ViewTreeLifecycleOwner immediately, IFF it was non-null. It frequently wasn't set yet, so stop() would never be called, and the callback would never be removed from the Jetpack dispatcher. Activity > Dispatcher > Detached Window, leak.

@rjrjr rjrjr merged commit 120cb47 into main Oct 31, 2022
@rjrjr rjrjr deleted the ray/leaky-backhandler branch October 31, 2022 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BackPressHandler leaks the view after its detached
4 participants