uber / AutoDispose

Automatic binding+disposal of RxJava streams.

Home Page:https://uber.github.io/AutoDispose/

Repository from Github https://github.comuber/AutoDisposeRepository from Github https://github.comuber/AutoDispose

Leak - DetachEventCompletable isn't removed when View is removed from ViewTree

ultraon opened this issue · comments

Hello, Thank you very much for that great library.

I think I found a leak when DetachEventCompletable.Listener isn't removed from listeners inside a View. And this view was multiple times added and removed from ViewTree.

Description:

  1. when this button is clicked, observe the number of listeners in
    android.view.View.ListenerInfo#mOnAttachStateChangeListeners using debug break-points
  2. Click the button "Add" and then "Remove" a few times
  3. Then observe the number of listeners in android.view.View.ListenerInfo#mOnAttachStateChangeListeners
    again

Result: the com.uber.autodispose.android.DetachEventCompletable listeners are not removed using
android.view.View.removeOnAttachStateChangeListener that should be done in
com.uber.autodispose.android.DetachEventCompletable.Listener.onViewDetachedFromWindow.

Probably the fix would be simple:

@Override
public void onViewDetachedFromWindow(View v) {
  if (!isDisposed()) {
    observer.onComplete();
    v.removeOnAttachStateChangeListener(this) // <---------- this is a fix.
  }
}

Sample project:
TestUberAutodisposeApp.zip

Nice catch! Want to send a PR?