sceneview-android icon indicating copy to clipboard operation
sceneview-android copied to clipboard

Segmentation Fault (SEGV_MAPERR) when destroying the view in Compose with 0.9.0

Open SalomonBrys opened this issue 2 years ago • 1 comments

I need to display a 3D arrow in my Compose UI.

Here is the quite simple code that I am using for that:

private class ArrowSceneView(context: Context) : SceneView(context) {

    // Disable touch & drag
    override val cameraGestureDetector: CameraGestureDetector? = null

    val arrowNode: ModelNode

    init {
        cameraNode.transform = lookAt(
            eye = Position(x = 0.0f, y = 0.0f, z = 0.8f),
            target = Position(0f, 0f, 0f),
            up = Direction(y = 1f)
        )

        arrowNode = ModelNode(rotation = Rotation(x = -90f))
        addChild(arrowNode)

        isTranslucent = true
    }

    suspend fun load() {
        arrowNode.loadModel(
            context = context,
            lifecycle = lifecycle,
            glbFileLocation = "arrow_light_gray.glb",
            scaleToUnits = 1.0f,
            centerOrigin = Position(x = 0.0f, y = 0.0f, z = 0.0f)
        )
    }

    fun setArrowRotation(azimuth: Float, elevation: Float) {
        arrowNode.smoothTransform = Quaternion.fromEuler(Rotation(x = -90f + elevation, -azimuth, 0f)).toMatrix()
    }

}

@Composable
fun Arrow3d(azimuth: Float, elevation: Float, modifier: Modifier = Modifier) {
    val coroutineScope = rememberCoroutineScope()
    AndroidView(
        factory = { context ->
            ArrowSceneView(context).also { view ->
                coroutineScope.launch { view.load() }
                view.setArrowRotation(azimuth, elevation)
            }
        },
        update = { view ->
            view.setArrowRotation(azimuth, elevation)
        },
        modifier = modifier
    )
}

This works perfectly, until I load another view, and this SceneView has to be destroyed. When that happens, I get the following crash:

2022-09-01 18:09:53.033 29427-29427/com.qorvo.uwb.android A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x6d0 in tid 29427 (rvo.uwb.android), pid 29427 (rvo.uwb.android)
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Build fingerprint: 'google/raven/raven:13/TP1A.220624.021/8877034:user/release-keys'
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Revision: 'MP1.0'
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: ABI: 'arm64'
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Timestamp: 2022-09-01 18:09:53.141892672+0200
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Process uptime: 12s
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Cmdline: com.qorvo.uwb.android
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: pid: 29427, tid: 29427, name: rvo.uwb.android  >>> com.qorvo.uwb.android <<<
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: uid: 10482
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x00000000000006d0
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: Cause: null pointer dereference
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x0  00000000000006c8  x1  0000000000000000  x2  0000000000000000  x3  00000074bd3e177c
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x4  000000740188eb8c  x5  0000000000000000  x6  0000000000000051  x7  0000007fca1ad918
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x8  02fb6cf1d647f1e1  x9  02fb6cf1d647f1e1  x10 0000000000000007  x11 0000000000000005
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x12 0000007fca1ad5b8  x13 0000007fca1ad470  x14 0000000000000002  x15 00000000ebad6a89
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x16 0000007419c747d0  x17 0000007fca1ae990  x18 0000007775b62000  x19 0000000000000000
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x20 0000000000000000  x21 0000000000000000  x22 00000074443d2904  x23 00000074447e9c2c
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x24 00000074bd200880  x25 0000007fca1aeaa0  x26 0000007fca1aeaac  x27 0000007fca1aeaa0
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     x28 0000007fca1ae9a0  x29 0000007fca1ae9a0
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:     lr  0000007419c6374c  sp  0000007fca1ae940  pc  0000007419c63e2c  pst 0000000060001000
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG: backtrace:
2022-09-01 18:09:53.331 29559-29559/? A/DEBUG:       #00 pc 00000000001b7e2c  /data/app/~~ho_nX2TnVkXRRpxQ2JNYhQ==/com.qorvo.uwb.android-gEJi4JJFKgNd2RCDrzQ6sA==/base.apk!libgltfio-jni.so (BuildId: c6c850b752925f24cdd0cecc117fd277bdf218c6)
2022-09-01 18:09:53.344 638-638/? E/tombstoned: Tombstone written to: tombstone_13

The crash happens with SceneView 0.9.0.

This code does not crash with SceneView 0.8.0.

SalomonBrys avatar Sep 01 '22 16:09 SalomonBrys

@SalomonBrys this crash happens due to implementation of

// Model.kt
fun Model.destroy() {
    releaseSourceData() // <--- crash ---|
    Filament.assetLoader.destroyAsset(this)
}

There's a call to FilamentAsset.releaseSourceData(), which is used more than once, but should not.

First call is here:

// GLBLoader.kt
fun createModel(lifecycle: Lifecycle? = null, buffer: Buffer): Model? =
        assetLoader.createAssetFromBinary(buffer)?.also { asset ->
            resourceLoader.loadResources(asset)
            //TODO: Used by Filament ModelViewer, see if it's useful
            asset.renderableEntities.forEach {
                it.setScreenSpaceContactShadows(true)
            }
            asset.releaseSourceData() // <--- first call ---|

            lifecycle?.observe(onDestroy = {
                // Prevent double destroy in case of manually destroyed
                runCatching { asset.destroy() } // <--- second call in this implementation ---|
            })
        }

Removing it from Model.destroy() should fix the problem.

aleyooop avatar Sep 10 '22 00:09 aleyooop

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Dec 10 '22 05:12 github-actions[bot]

This should probably stay open until the next release is cut with a fix, whether #150 gets merged or the code moves away from lifecycle events (as indicated was the plan in said PR).

At the very least, I'd like it to remain open since it's how I'm keeping track as to if this is still an issue or not :)

TheDukeofErl avatar Dec 13 '22 18:12 TheDukeofErl

What are the plans on getting this issue resolved? It's holding up a production version of my app using this library.

ryust avatar Jan 12 '23 23:01 ryust

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Apr 13 '23 05:04 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

github-actions[bot] avatar Apr 20 '23 05:04 github-actions[bot]