androidsegmentation-faultarcore

ArCore Segmentation-fault on update


Im trying to use google's ArCore kit, but my app crashes on Session.update() with error

A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 20724 (GLThread 529), pid 20685 (...)

AndroidManifest.xml

...
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-feature android:name="android.hardware.camera.ar" />
<uses-permission android:name="android.permission.CAMERA" />
...
<application>
    ...
    <meta-data android:name="com.google.ar.core" android:value="required" />
</application>
...

MainActivity.kt

I check availability as described here

MainFragment.kt

class MainFragment : Fragment(R.layout.fragment_main), GLSurfaceView.Renderer {
    private lateinit var surfaceView: GLSurfaceView

    private lateinit var session: Session

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        surfaceView = view.findViewById(R.id.surface_view)
        surfaceView.setRenderer(this)
        surfaceView.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY

        session = Session(requireContext())
        val config = Config(session)
        session.configure(config)
    }

    override fun onResume() {
        super.onResume()
        session.resume()
    }

    override fun onPause() {
        super.onPause()
        session.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        session.close()
    }

    override fun onSurfaceCreated(gl: GL10, cfg: EGLConfig) {
        GLES20.glClearColor(1f, 1f, 1f, 1f)
        val texture = intArrayOf(-1)
        GLES20.glGenTextures(1, texture, 0)
        if (texture[0] < 0) {
            throw Exception("Failed to Create OpenGL texture")
        }
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0])
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR )
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR)
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0)
        session.setCameraTextureName(texture[0])
    }

    override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        session.update()
    }
}

When I create an OpenGL context manually using EGL in a sepeate thread and initialize, use an ArCore session inside it everything works fine, but it's a redundant piece of code as we have GLSurfaceView.Renderer.


Solution

  • After looking into a sample provided by google, I've found what was causing the crash, it looks like before calling GLSurfaceView.setRenderer() you must call GLSurfaceView.setEGLContextClientVersion() for ar.core.Session to work properly.