SceneView / sceneview-android

SceneView is a 3D and AR Android Composable and View with Google Filament and ARCore. This is a Sceneform replacement in Kotlin
Apache License 2.0
758 stars 151 forks source link

NDK crash in ARScene on version 2.0.4 #435

Closed Antiglobalist closed 1 month ago

Antiglobalist commented 4 months ago

I have described it in PR(Unfortunately it is late :) ) https://github.com/SceneView/sceneview-android/pull/408#issuecomment-1980569591

sameerjj commented 4 months ago

Hi @Antiglobalist in your comment you had this code: image are you still calling viewBinding?.sceneView.session.resume() manually?

Antiglobalist commented 4 months ago

Hello It's strange that this screenshot remains, since I edited my comment :)

Yes I used it. However, when I started looking into this problem, I discovered that these calls is calling under the hood. And I deleted my calls But that wasn't the reason for the exception

So i checked it again on the Sample project It turned out that arCore.destroy() is not called there, precisely because of the reason that I warned about in my comment image

In my Project flow Scope is alive and arCore.destroy() is calling twice. And I assume it is root of exception

Antiglobalist commented 4 months ago

You can reproduce it by this code

private val testAppScope = MainScope()
    fun destroyARCore() {
        testAppScope.launch(Dispatchers.IO) {
            // destroy should be called off the main thread since it hangs for many seconds
            arCore.destroy()
        }
    }

So it requires two changes: 1 - Sync the destroy block and do not call it again 2 - I am not sure about coroutines for execute that. It seems like a simple way to do it, but maybe change it to ExecutorService?

sameerjj commented 4 months ago

I wouldn't call session.pause() or session.resume() I believe the lifecycle takes care of those for us. Does this crash happen every time for you? I definitely experience an every-time crash while testing this fix

Antiglobalist commented 4 months ago

I wouldn't call session.pause() or session.resume() I believe the lifecycle takes care of those for us. You are right. As I said I had removed it. Thk you! Yes it is stable crash on Samsung and Pixel 6

You can try to apply the changes that i wrote upper Test case: 1 - Start Activity(Run sample app) 2 - Press back (Finish activity) 3 - Look at the Crash in logs

sameerjj commented 4 months ago

Sorry @Antiglobalist I skimmed over the part where you said you removed those custom calls. Can you confirm this issue is new in 2.0.4, and was not present in 2.0.3? If you can pinpoint what version this begins to occur, it can help us determine a fix - if destroy is being called twice, then wouldn't this cause an issue regardless of the use of scope.launch?

Antiglobalist commented 4 months ago

Sure Previously I used 2.0.3 version and it was working well

To confirm it, I added a flag so that there is no double call And now there is no crash We need to implement a similar behavior

` private val testAppScope = MainScope() private var isDestroying = false

fun destroyARCore() {
    if (!isDestroying) {
        isDestroying = true
        testAppScope.launch(Dispatchers.IO) {
            // destroy should be called off the main thread since it hangs for many seconds
            arCore.destroy()
        }
    }
}`

image

Antiglobalist commented 4 months ago

@sameerjj I have created PR. You can check it. Thk you for conversation

kmayoral commented 3 months ago

Hi all, thanks for the work here! Just a heads up that I still see NDK crashes coming from ARCore when calling destroy which seem to indicate a double call. I'm trying to investigate on my end but just wanted to mention it.

I see the issue when building the latest from main branch (corresponding to release 2.1.0).

kmayoral commented 3 months ago

I forgot to share the stacktrace I'm seeing:

2024-03-29 10:30:44.457 10041-10041 SceneViewActivity      com.test.android                      I  onDestroyed()
2024-03-29 10:30:44.459 10041-10041 WindowOnBackDispatcher  com.test.android                      W  sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda11@a63e9dc
2024-03-29 10:30:44.460 10041-10041 Sceneview               com.test.android                      D  CameraStream destroyed
        2024-03-29 10:30:44.462 10041-10924 native                  com.test.android                      I  I0000 00:00:1711733444.462112   10924 session_lite_c_api.cc:37] Deleting ArSession...
2024-03-29 10:30:44.462 10041-10041 VRI[SceneViewActivity] com.test.android                      D  applyTransactionOnDraw applyImmediately
2024-03-29 10:30:44.462 10041-10041 VRI[SceneViewActivity] com.test.android                      D  applyTransactionOnDraw applyImmediately
2024-03-29 10:30:44.489 10041-10852 native                  com.test.android                      I  I0000 00:00:1711733444.489113   10852 jni_util.cc:91] Exiting thread. Detach thread.
2024-03-29 10:30:44.503 10041-10921 native                  com.test.android                      I  I0000 00:00:1711733444.503732   10921 session_lite_c_api.cc:39] Deleted ArSession.
2024-03-29 10:30:44.666 10041-10920 libc                    com.test.android                      A  Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 10920 (pool-73-thread-), pid 10041 (com.test.android)
2024-03-29 10:30:44.981 10041-10282 SceneViewPresenter com.test.android                      D  received location status update, computing changes now
---------------------------- PROCESS STARTED (10930) for package com.test.android ----------------------------
2024-03-29 10:30:45.232 10041-10282 SceneViewPresenter com.test.android                      D  received location status update, computing changes now
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A  Cmdline: com.test.android
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A  pid: 10041, tid: 10920, name: pool-73-thread-  >>> com.test.android <<<
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #09 pc 0000000000015a14  /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!libarcore_sdk_jni.so (offset 0x5f09000) (BuildId: bc7c9a1d0fb51f4e02e11d58f89da0f2)
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #15 pc 000000000022c000  [anon:dalvik-classes38.dex extracted in memory from /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!classes38.dex] (com.google.ar.core.Session.close+0)
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #20 pc 0000000000073f88  [anon:dalvik-classes2.dex extracted in memory from /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!classes2.dex] (io.github.sceneview.ar.ARCore.destroy+0)
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #25 pc 0000000000078b54  [anon:dalvik-classes2.dex extracted in memory from /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!classes2.dex] (io.github.sceneview.ar.ARSceneView.destroyArCore$lambda$29+0)
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #30 pc 0000000000076e60  [anon:dalvik-classes2.dex extracted in memory from /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!classes2.dex] (io.github.sceneview.ar.ARSceneView.$r8$lambda$gQjYzKfEex6OJBQDuxApIG4a5lM+0)
2024-03-29 10:30:45.366 10928-10928 DEBUG                   crash_dump64                         A        #35 pc 000000000007627c  [anon:dalvik-classes2.dex extracted in memory from /data/app/~~Fe0Vrwf_mqGKced8UpRAng==/com.test.android-pAqPMH9ahG_JOE-1M9j5jA==/base.apk!classes2.dex] (io.github.sceneview.ar.ARSceneView$$ExternalSyntheticLambda0.run+0)
kmayoral commented 3 months ago

@sameerjj / @Antiglobalist I just submitted a PR change which appears to fix the issue on my end if you'd like to review, thanks!

https://github.com/SceneView/sceneview-android/pull/451

Thanks all and @ThomasGorisse for the great library!

Antiglobalist commented 3 months ago

@kmayoral Hi there! Pls look at my comment in your PR

ThomasGorisse commented 1 month ago

Reopen if it's still the case in v2.2.0

kmayoral commented 1 month ago

Hopefully it's still ok since it wasn't an issue in v2.1.1 where #451 was originally released but 🤞 , thanks again!