title: Error: "Fatal signal 11 (SIGSEGV), code 1" when passing Mat object from java to jni function
body: <p>I am running the video camera using the <code>OpenCV</code> function. I pass the <code>Mat</code> object to the <code>jni</code> function it works for awhile, them the error:</p>
  
  <pre><code>10-10 13:03:17.978: A/libc(28693): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x9 in tid 28791 (Thread-5418)
  </code></pre>
  
  <p>Java code that runs the camera and calls the jni function:</p>
  
  <pre><code>package com.adhamenaya;
  
  import java.util.ArrayList;
  
  import org.opencv.android.BaseLoaderCallback;
  import org.opencv.android.CameraBridgeViewBase;
  import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
  import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
  import org.opencv.android.LoaderCallbackInterface;
  import org.opencv.android.OpenCVLoader;
  import org.opencv.core.Mat;
  //import org.opencv.highgui.Highgui;
  import org.opencv.imgproc.Imgproc;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.View.OnTouchListener;
  import android.view.WindowManager;
  
  public class MainActivity extends Activity implements CvCameraViewListener2,
          OnTouchListener {
  
      private static final String TAG = "OCVSample::Activity";
      private Mat mRgba;
      private Mat mGray;
      private CameraBridgeViewBase mOpenCvCameraView;
  
      private ArrayList&lt;Mat&gt; mats = new ArrayList&lt;Mat&gt;();
  
      private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
          @Override
          public void onManagerConnected(int status) {
              switch (status) {
              case LoaderCallbackInterface.SUCCESS: {
                  Log.i(TAG, "OpenCV loaded successfully");
                  mOpenCvCameraView.enableView();
                  mOpenCvCameraView.setOnTouchListener(MainActivity.this);
  
              }
                  break;
              default: {
                  super.onManagerConnected(status);
              }
                  break;
              }
          }
      };
  
      public MainActivity() {
          Log.i(TAG, "Instantiated new " + this.getClass());
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          Log.i(TAG, "called onCreate");
          super.onCreate(savedInstanceState);
  
          Native.loadlibs();
  
  
          mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.cam_view);
          mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
          mOpenCvCameraView.setCvCameraViewListener(this);
  
          Native.setup(mFaceCascadeFile, mNoseCascadeFile, mLandmarks);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          if (mOpenCvCameraView != null)
              mOpenCvCameraView.disableView();
      }
  
      @Override
      public void onResume() {
          super.onResume();
          OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
                  mLoaderCallback);
      }
  
      public void onDestroy() {
          super.onDestroy();
          if (mOpenCvCameraView != null)
              mOpenCvCameraView.disableView();
      }
  
      public void onCameraViewStarted(int width, int height) {
          mGray = new Mat();
          mRgba = new Mat();
      }
  
      public void onCameraViewStopped() {
      }
  
      public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
          mRgba = inputFrame.rgba();
          Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGRA2GRAY);
          Native.runJni(mFaceCascadeFile, mNoseCascadeFile, mLandmarks,
                  mRgba.getNativeObjAddr());
  
          return mRgba;
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          // TODO Auto-generated method stub
          return false;
      }
  
  
  }
  </code></pre>
  
  <p>Jni function:</p>
  
  <pre><code>JNIEXPORT jbyteArray JNICALL Java_com_adhamenaya_Native_runJni(JNIEnv * env,
          jobject obj, jstring faceCascadeFile, jstring noseCascadeFile,
          jstring landmarks, jlong frame) {
  
      cv::Mat&amp; inFrame = *(cv::Mat*) frame;
  
      if (!gsys.loadFaceCascade(faceCascadeFnameStr)) {
          LOG("Could not load face cascade");
          gsys.loadFaceCascade(faceCascadeFnameStr);
      } else {
          LOG("Face cascade: OK");
  
      }
  
      if (!gsys.loadNoseCascade(noseCascadeFnameStr)) {
          LOG("Could not load nose cascade");
          gsys.loadNoseCascade(noseCascadeFnameStr);
  
      } else {
          LOG("Nose cascade: OK");
  
      }
  
      gsys.setFrameRate(30);
      gsys.setProgramState(DETECT);
  
      clock_t tin, tout = 0;
  
      cv::flip(inFrame, inFrame, 0);
      cv::transpose(inFrame, inFrame);
  
      dlib::shape_predictor pose_model;
      dlib::deserialize(landmarksStr) &gt;&gt; pose_model;
  
  
       gsys.setCurrentFrame(inFrame);
  
       tin = clock();
       trigger_hr(gsys, faces, pose_model);
  
       // Process the frame
       size_t spm;
       float motionStrengthX, motionStrengthY;
       float phiYaw = -0xFFFFFFFF, thetaPitch = -0xFFFFFFFF;
  
       if (faces.size()) {
       faces[0].getSpm(gsys, spm, motionStrengthX, motionStrengthY);
       faces[0].getFacePose(phiYaw, thetaPitch);
       }
  
       tout = tout + clock() - tin;
       if ((gsys.getFrameCount() % 30) == 29) {
       double secs_between_frames = (double) (tout) / (CLOCKS_PER_SEC * 30.0f);
       printf("FPS = %2.2f\n", 1.0f / secs_between_frames);
       LOG("FPS = %2.2f ", 1.0f / secs_between_frames);
       tout = 0;
       }
  
       char spmText[100];
  
       //sprintf(spmText,
       //     "SPM = %zu, P = %2.2f, T = %2.2f, MS-X = %2.2f, MS-Y = %2.2f", spm,
       //     phiYaw, thetaPitch, motionStrengthX, motionStrengthY);
  
       LOG("SPM = %zu, P = %2.2f, T = %2.2f, MS-X = %2.2f, MS-Y = %2.2f", spm,
       phiYaw, thetaPitch, motionStrengthX, motionStrengthY);
  
      std::string str;
      str = "SPM=";
  
      jbyteArray arr = env-&gt;NewByteArray(str.length());
      env-&gt;SetByteArrayRegion(arr, 0, str.length(), (jbyte*) str.c_str());
  
      return arr;
  
  }
  </code></pre>
  
  <p>Kindly help me.</p>