Skip to content

[Solution] SIGSEGV when closing JavaCamera2View #13574

@ValkA

Description

@ValkA
  • OpenCV => 3.4.4
  • Operating System / Platform => Android 8.0.0, Galaxy S9
  • Compiler => Android Studio
Detailed description

I've created calibration activity (similar to the one in the samples) but changed to JavaCamera2View.
When exiting the activity with finish(), camera background thread is still running after releasing mImageReader and thus I was randomly getting SIGSEGV with the following native crash dump:

********** Crash dump: **********
Build fingerprint: 'samsung/starltexx/starlte:8.0.0/R16NW/G960FXXU2BRJ3:user/release-keys'
pid: 32293, tid: 32493, name: OpenCVCameraBac  >>> com.valka.reversecamera <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x78020dd000
Stack frame #00 pc 000000000001dfd0  /system/lib64/libc.so (memcpy+384)
Stack frame #01 pc 00000000003762bc  /data/app/com.valka.reversecamera-NRoygpkdLqtU2Iqpol1bOQ==/lib/arm64/libopencv_java3.so (_ZNK2cv3Mat6copyToERKNS_12_OutputArrayE+636)
Stack frame #02 pc 0000000000a4fd88  /data/app/com.valka.reversecamera-NRoygpkdLqtU2Iqpol1bOQ==/lib/arm64/libopencv_java3.so (_ZN2cv21findChessboardCornersERKNS_11_InputArrayENS_5Size_IiEERKNS_12_OutputArrayEi+7208)
Stack frame #03 pc 00000000001b32d4  /data/app/com.valka.reversecamera-NRoygpkdLqtU2Iqpol1bOQ==/lib/arm64/libopencv_java3.so (Java_org_opencv_calib3d_Calib3d_findChessboardCorners_10+116)
Stack frame #04 pc 0000000000016c58  /data/app/com.valka.reversecamera-NRoygpkdLqtU2Iqpol1bOQ==/oat/arm64/base.odex (offset 0x11000)
Steps to reproduce

Take camera calibration example, change to JavaCamera2View instead of JavaCameraView and start opening/closing the activity until it crashes.

My solution

In this file:
https://github.com/opencv/opencv/blob/b39cd06249213220e802bb64260727711d9fc98c/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java
in function disconnectCamera(), first mImageReader is freed then stopBackgroundThread() is called.

Changing the order makes sense and it didn't crash for me anymore.

    @Override
    protected void disconnectCamera() {
        Log.i(LOGTAG, "close camera");
        try {
            CameraDevice c = mCameraDevice;
            mCameraDevice = null;
            if (null != mCaptureSession) {
                mCaptureSession.close();
                mCaptureSession = null;
            }
            if (null != c) {
                c.close();
            }
        } finally {
            stopBackgroundThread();
            if (null != mImageReader) {
                mImageReader.close();
                mImageReader = null;
            }
        }
        Log.i(LOGTAG, "camera closed!");
    }

Hope it helps someone.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions