SIGSEGV on OpenCV JNI from Android

I'm trying to run a piece of code through OpenCV Java, then pass the Mat object to OpenCV JNI code which does Canny Edge detection on it and returns the Mat. But somehow, I'm repeatedly getting a SIGSEGV when the app launches and I'm unsure why this is:

09-23 00:30:19.501 20399-20547/com.example.opencv.opencvtest A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x3 in tid 20547 (Thread-7450)

The Java code segment in question is:

@Override
public void onCameraViewStarted(int width, int height) {
    // Everything initialized 
    mGray = new Mat(height, width, CvType.CV_8UC4);
    mGauss = new Mat(height, width, CvType.CV_8UC4);
    mCanny = new Mat(height, width, CvType.CV_8UC4);
}    

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mGray = inputFrame.rgba();
    Imgproc.GaussianBlur(mGray, mGauss, new Size(), 5);

    // This works perfectly fine
    // Imgproc.Canny(mGauss, mCanny, 0, 20);

    // But this causes a SIGSEGV
    nativeCanny(mGauss.getNativeObjAddr(), mCanny.getNativeObjAddr());
    return mCanny;
}

The JNI code is:

 extern "C" {
 JNIEXPORT jboolean JNICALL
 Java_com_example_opencv_opencvtest_MainActivity_nativeCanny(JNIEnv *env, jobject instance, long iAddr, long oAddr) {

    cv::Mat* blur = (cv::Mat*) iAddr;
    cv::Mat* canny = (cv::Mat*) oAddr;

    // This line is causing the SIGSEGV because if I comment it, 
    // everything works (but Mat* canny is empty so shows up black screen)
    Canny(*blur, *canny, 10, 30, 3 );

    return true;
  }
}

Any idea why this is happening? I've spent the better half of the day trying to figure out why this is breaking but made no headway other than isolating the problematic statements.

EDIT: From the comments

I think it was an error with the initialization of mCanny. If I change the JNI call to Canny(*blur, *blur, 10, 30, 3 ); and then in Java return mGauss instead of mCanny then it works fine. This fixes it for the moment, but I'm honestly still unsure why mCanny is causing the SIGSEGV.

Answers


SEGV means you tried to read/write unallocated memory. The fault address is 3. Something that close to 0 almost always means you dereferenced a null pointer. My guess is that either mGauss or mCanny had a 0 for their native object addr.


Need Your Help

How to capture image from webcam WHILST already streaming from webcam too with Java Gstreamer?

java webcam gstreamer java-gstreamer

I am using the gstreamer library for a Java project that needs to be able to capture an image from a webcam.

Lowering process priority of multiprocessing.Pool on Windows

python python-2.7 pandas multiprocessing

I use multiprocessing.Pool() to parallelize some heavy Pandas processing but find that it is a bit too successful. My CPU usage goes to 100% and my entire computer becomes very unresponsive. Even the