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

Null sourceObserver in MvRxLifecyclewareObserver onDestroy to fix leak #111

Merged
merged 2 commits into from
Oct 8, 2018

Conversation

BenSchwab
Copy link
Collaborator

@BenSchwab BenSchwab commented Oct 3, 2018

We maintain a collection of disposables in a viewmodel which are disposed onClear:

  /**
     * Override this to provide the initial state.
     */
    @CallSuper
    override fun onCleared() {
        super.onCleared()
        disposables.dispose()
    }

However, MvRxLifecycleAwareObserver maintains a reference to a source observer (often fragment). While we dispose that subscription in onDestroy of the fragment, the CompositeObservable in BaseMvRxViewModel retains a reference to it, leaking the fragment until onClear is called for the ViewModel. If you rotate your phone a bunch, you will have many fragments referenced via this chain before onClear is called.

Copy link
Collaborator

@gpeal gpeal left a comment

Choose a reason for hiding this comment

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

@gpeal
Copy link
Collaborator

gpeal commented Oct 4, 2018

Nice catch!

Copy link
Contributor

@hellohuanlin hellohuanlin left a 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,
Copy link
Contributor

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?

Copy link
Collaborator Author

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
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

LGTM

@BenSchwab BenSchwab merged commit 1acbbf4 into master Oct 8, 2018
@BenSchwab BenSchwab deleted the bschwab--observer-leak branch December 19, 2018 01:35
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.

3 participants