Skip to content

R8 Minification Results in ViewModel Crash onDestroy #727

@AlexanderGH

Description

@AlexanderGH

When running our app with full R8 minification, I suspect some re-ordering or inlining results in an invalid call to a lifecycle state change if a GC is run somewhere in the middle.

java.lang.IllegalStateException: LifecycleOwner of this LifecycleRegistry is already garbage collected. It is too late to change lifecycle state.
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry:65346)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry:65349)
        at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry:65347)
        at com.airbnb.mvrx.BaseMvRxViewModel.onCleared(BaseMvRxViewModel:65352)
        at <our view model>.onCleared(:65343)
        at com.airbnb.mvrx.MavericksViewModelWrapper.onCleared(MavericksViewModelWrapper:65353)
        at androidx.lifecycle.ViewModel.clear$lifecycle_viewmodel_release(ViewModel:65353)
        at androidx.lifecycle.ViewModelStore.clear(ViewModelStore:65353)
        at androidx.fragment.app.FragmentManagerViewModel.clearNonConfigStateInternal(FragmentManagerViewModel:65351)
        at androidx.fragment.app.FragmentStateManager.destroy(FragmentStateManager:65350)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager:65346)
        at androidx.fragment.app.SpecialEffectsController$FragmentStateManagerOperation.complete$fragment_release(SpecialEffectsController:65352)
        at androidx.fragment.app.SpecialEffectsController$FragmentStateManagerOperation.cancel(SpecialEffectsController:65353)
        at androidx.fragment.app.DefaultSpecialEffectsController.forceCompleteAllOperations(DefaultSpecialEffectsController:65346)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager:65329)
        at androidx.fragment.app.FragmentManager.dispatchDestroy(FragmentManager:65338)
        at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity:65344)
        at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity:65338)
        at <our activity>.onDestroy(:65328)
        at android.app.Activity.performDestroy(Activity.java:9460)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1556)

Ignore the line numbers being all wonky due to R8.

My blind theory is that in onCleared of BaseMvRxViewModel, that the disposing and cleanup should possible go before the super() call?

   public void onCleared() {
      super.onCleared();
      this.disposables.dispose();
      this.lifecycleRegistry.setCurrentState(State.DESTROYED);
   }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions