-
Notifications
You must be signed in to change notification settings - Fork 500
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
Null sourceObserver in MvRxLifecyclewareObserver onDestroy to fix leak #111
Conversation
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.
Nice catch! |
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.
LGTM!
private val activeState: Lifecycle.State = DEFAULT_ACTIVE_STATE, | ||
private val alwaysDeliverLastValueWhenUnlocked: Boolean = false, | ||
private val sourceObserver: Observer<T>) : AtomicReference<Disposable>(), LifecycleObserver, Observer<T>, Disposable { | ||
owner: LifecycleOwner, |
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.
can we make the owner
a private var
and optional?
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.
Oh thanks for pointing this out. We can make the constructor take non-null types, but make the main constructor take private, nullable types.
if (!isDisposed) { | ||
dispose() | ||
} | ||
owner = null | ||
sourceObserver = null |
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.
👍
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.
LGTM
We maintain a collection of disposables in a viewmodel which are disposed onClear:
However,
MvRxLifecycleAwareObserver
maintains a reference to a source observer (often fragment). While we dispose that subscription inonDestroy
of the fragment, theCompositeObservable
inBaseMvRxViewModel
retains a reference to it, leaking the fragment untilonClear
is called for the ViewModel. If you rotate your phone a bunch, you will have many fragments referenced via this chain beforeonClear
is called.