rive-app / rive-app/rive-android

Crash when coming back into screen with custom view that has rive view on it

Open
#369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
Kotlin
Stars
538
Forks
66
PR merge metrics
No merged PRs in 30d

Description

Description

I've discovered a crash on a project, it basically happens when going away from a Fragment that has a rive view embedeed into some custom xml view and that one is used on the fragment layout. What happens is that if I navigate away and then come back using some kind of onBack function (either user pressed or called from the code) then the app crashes due to some rive process.

We've had issues with rive native references in the past so we are already using some logic to play around this to increment the reference count into the rive views in the fragment like:


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        if (_binding != null) {
            // Entering here means we are restoring the fragment from backstack.
            restoringStateFromBackStack = true
            _binding.riveView.controller.acquire()
            return binding.root
        }
        _binding = FragmentMainBinding.inflate(inflater, container, false)

        return binding.root
    }

Another important thing to mention is that we don't relaunch a new fragment after coming back to it, we reuse the existing one. So problably the issue lies around the expectation of the fragment being fully destroyed but then when coming back to it the references are already disposed but we still need them.

Provide a Repro

I haven't been able to repro on a separate project, so I can't provide a functiona repro at this moment.

Source .riv/.rev file

It's not related not any specific file, it happens with any riv file we try to use.

Expected behavior

The app doesn't crash when going back to previous screen.

Device & Versions (please complete the following information)
  • Device: Reproducible on real devices and emulators. I've been mostly using emulator Pixel 8 Pro.
  • SDK Level API 35
  • We are currently using 8.4.0 and tried to update the library to the latest, but saw this crash and through trial an error we see that it's started on version 8.6.0.
Additional context

Here is the full stack trace of the error, it seems like something happens when rebuilding the window but the rive view already discharged the native dependencies.

FATAL EXCEPTION: main
                 Process: com.app.example, PID: 18741
                 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:590)
                 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
                 Caused by: java.lang.reflect.InvocationTargetException
                 	at java.lang.reflect.Method.invoke(Native Method)
                 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886) 
                 Caused by: app.rive.runtime.kotlin.core.errors.RiveException: C++ object for app.rive.runtime.kotlin.core.FallbackAssetLoader@216590208 does not exist. See MEMORY_MANAGEMENT.md for more information.
                 	at app.rive.runtime.kotlin.core.NativeObject.getCppPointer(NativeObject.kt:39)
                 	at app.rive.runtime.kotlin.core.FileAssetLoader.setRendererType(FileAssetLoader.kt:46)
                 	at app.rive.runtime.kotlin.core.File.<init>(File.kt:28)
                 	at app.rive.runtime.kotlin.RiveAnimationView.loadFileFromResource(RiveAnimationView.kt:403)
                 	at app.rive.runtime.kotlin.RiveAnimationView.onAttachedToWindow(RiveAnimationView.kt:855)
                 	at android.view.View.dispatchAttachedToWindow(View.java:22860)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3499)
                 	at android.view.ViewGroup.addViewInner(ViewGroup.java:5299)
                 	at android.view.ViewGroup.addView(ViewGroup.java:5085)
                 	at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.kt:269)
                 	at android.view.ViewGroup.addView(ViewGroup.java:5025)
                 	at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:893)
                 	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:577)
                 	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
                 	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2094)
                 	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2004)
                 	at androidx.fragment.app.FragmentManager.popBackStackImmediate(FragmentManager.java:1052)
                 	at androidx.fragment.app.FragmentManager.popBackStackImmediate(FragmentManager.java:943)
                 	at androidx.fragment.app.FragmentManager.handleOnBackPressed(FragmentManager.java:859)
                 	at androidx.fragment.app.FragmentManager$1.handleOnBackPressed(FragmentManager.java:529)
                 	at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.kt:276)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RiveAnimationView.onAttachedToWindow and loadFileFromResource, then trace File.kt, FileAssetLoader.kt, and NativeObject.kt using the supplied stack trace and fragment back-stack lifecycle. Reproduce the navigation scenario if possible and verify that returning to a fragment with an embedded Rive view no longer crashes when native dependencies have been disposed.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.