title: How to resolve Fatal signal 11 (SIGSEGV) code 1 in Android?
body: <p>I am developing an Android application using Opencv for image processing.</p>
  
  <p>The processing part is called in java through JNI functions.</p>
  
  <p>I am sending to c++ two Opencv <code>Mat</code> objects to find matching points.</p>
  
  <p>The problem is that sometimes the app works and sometimes it doesn't even while using the same images.</p>
  
  <p>I already check if the images aren't empty and that they are valid before sending them to the native part.</p>
  
  <p>Here is the stack trace is get in Log </p>
  
  <pre><code>2019-04-26 12:52:06.980 24955-25666/com.grimg.coffretpfe A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 25666 (Thread-10)
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: Build fingerprint: 'samsung/zeroltexx/zerolte:7.0/NRD90M/G925FXXU6ERF5:user/release-keys'
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: Revision: '10'
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: ABI: 'arm'
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: pid: 24955, tid: 25666, name: Thread-10  &gt;&gt;&gt; com.grimg.coffretpfe &lt;&lt;&lt;
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
  2019-04-26 12:52:07.078 25766-25766/? A/DEBUG:     r0 00000000  r1 00000000  r2 00000000  r3 b90a641c
  2019-04-26 12:52:07.079 25766-25766/? A/DEBUG:     r4 ffffffff  r5 c09a4128  r6 b90a678c  r7 b90a6670
  2019-04-26 12:52:07.079 25766-25766/? A/DEBUG:     r8 d33ee570  r9 97df7155  sl b90a6790  fp c06e3900
  2019-04-26 12:52:07.079 25766-25766/? A/DEBUG:     ip c5ec4840  sp b90a6268  lr c5eb8043  pc c5eb8042  cpsr 600f0030
  2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: backtrace:
  2019-04-26 12:52:07.081 25766-25766/? A/DEBUG:     #00 pc 00014042  /data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (_Z6toGrayN2cv3MatES0_+1577)
  2019-04-26 12:52:07.081 25766-25766/? A/DEBUG:     #01 pc 00014d65  /data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures+124)
  2019-04-26 12:52:07.081 25766-25766/? A/DEBUG:     #02 pc 000adc99  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
  2019-04-26 12:52:07.081 25766-25766/? A/DEBUG:     #03 pc 0000b0dd  /dev/ashmem/dalvik-jit-code-cache_24955_24955 (deleted)
  </code></pre>
  
  <p>I am running my app on a Samsung s6 edge, if this matters.</p>
  
  <p>My native part contains two functions, <code>toGray</code> and here is the signature </p>
  
  <pre><code>double toGray(Mat captured, Mat target)
  </code></pre>
  
  <p>I didnt added the code because it's a lot of processing and operations</p>
  
  <p>And the method I call in java is <code>detectFeatures</code></p>
  
  <pre><code>extern "C" {
  JNIEXPORT
  jdouble
  JNICALL Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures(
      JNIEnv *env,
      jobject instance,
      jlong addrRgba,
      jlong addrGray) {
  
   if (addrGray != NULL &amp;&amp; addrRgba != NULL) {
      cv::Mat &amp;mRgb = *(cv::Mat *) addrRgba;
      cv::Mat &amp;mGray = *(cv::Mat *) addrGray;
  
  
      jdouble retVal;
  
      double conv = toGray(mRgb, mGray);
  
      retVal = (jdouble) conv;
  
      return retVal;
    }
  
     return -1;
   }
   }
  </code></pre>
  
  <p>I tried every available solution over here , but nothing works ,besides I don't really understand the problem so I don't know what to search for.</p>
  
  <p>I hope you guys can help me solve this problem. Thanx in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>double toGray(Mat captured, Mat target) {
  std::vector&lt;cv::KeyPoint&gt; keypointsCaptured;
  std::vector&lt;cv::KeyPoint&gt; keypointsTarget;
  
  cv::Mat descriptorsCaptured;
  cv::Mat descriptorsTarget;
  //cv::Mat captured;
  std::vector&lt;cv::DMatch&gt; matches;
  std::vector&lt;cv::DMatch&gt; symMatches;
  
  //std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches1;
  //std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches2;
  //Mat captured, target;
  /*captured = imread("/storage/emulated/0/data/img1.jpg", IMREAD_GRAYSCALE);
  target = imread("/storage/emulated/0/data/img3.jpg", IMREAD_GRAYSCALE);
  if (!captured.data) {
      // Print error message and quit
      __android_log_print(ANDROID_LOG_INFO, "sometag", "I cant do this.");
  
  }
  if (!target.data) {
      // Print error message and quit
      __android_log_print(ANDROID_LOG_INFO, "sometag", "cant do nuthin.");
  
  
  }*/
  //cvtColor(capturedR, captured, CV_RGBA2GRAY);
  //cvtColor(targetR, target, CV_RGBA2GRAY);
  
  
  orb = ORB::create();
  
  //Pre-process
  resize(captured, captured, Size(480, 360));
  medianBlur(captured, captured, 5);
  
  resize(target, target, Size(480, 360));
  medianBlur(target, target, 5);
  
  orb-&gt;detectAndCompute(captured, noArray(), keypointsCaptured, descriptorsCaptured);
  orb-&gt;detectAndCompute(target, noArray(), keypointsTarget, descriptorsTarget);
  //__android_log_print(ANDROID_LOG_INFO, "sometag", "keypoints2 size = %d", keypointsTarget.size());
  //__android_log_print(ANDROID_LOG_INFO, "sometag", "keypoints size = %d", keypointsCaptured.size());
  
  //Match images based on k nearest neighbour
  std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches1;
  matcher.knnMatch(descriptorsCaptured, descriptorsTarget,
                   matches1, 2);
  //__android_log_print(ANDROID_LOG_INFO, "sometag", "Matches1 = %d",     matches1.size());
  std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches2;
  matcher.knnMatch(descriptorsTarget, descriptorsCaptured,
                   matches2, 2);
  //Ratio filter
  ratioTest(matches1);
  ratioTest(matches2);
  symmetryTest(matches1, matches2, symMatches);
  ransacTest(symMatches,
             keypointsCaptured, keypointsTarget, matches);
  const int symMatchCount = matches.size();
  
  Point2f point1;
  Point2f point2;
  float median;
  float meanBoy = 0;
  float greatest = 0;
  float lowest = 0;
  int count = 0;
  vector&lt;float&gt; angleList;
  vector&lt;Point2f&gt; point1List;
  vector&lt;Point2f&gt; point2List;
  
  for (int i = 0; i &lt; matches.size(); i++) {
      point1 = keypointsCaptured[matches[i].queryIdx].pt;
      point2 = keypointsTarget[matches[i].trainIdx].pt;
      point1List.push_back(point1);
      point2List.push_back(point2);
  
      deltaY = ((360 - point2.y) - (360 - point1.y));
      deltaX = (point2.x + 480 - point1.x);
  
      angle = atan2(deltaY, deltaX) * 180 / PI;
      cout &lt;&lt; "ORB Matching Results" &lt;&lt; angle &lt;&lt; endl;
      //if (angle &gt; greatest) greatest = angle;
      //if (angle &lt; lowest) lowest = angle;
      meanBoy += angle;
  
      angleList.push_back(angle);
      //std::cout &lt;&lt; "points " &lt;&lt; "(" &lt;&lt; point1.x &lt;&lt; "," &lt;&lt;360-point1.y&lt;&lt;") (" &lt;&lt; point2.x &lt;&lt; ","&lt;&lt;360-point2.y&lt;&lt;") angle:" &lt;&lt;angle &lt;&lt; std::endl;
      //std::cout &lt;&lt; angle &lt;&lt; std::endl;
  
  }
  // do something with the best points...
  
  //std::cout &lt;&lt; "Mean" &lt;&lt; meanBoy/symMatchCount &lt;&lt; std::endl;
  vector&lt;float&gt; angleLCopy(angleList);
  std::sort(angleLCopy.begin(), angleLCopy.end());
  /*               if(angleList.size() % 2 == 0)
                           median = (angleList[angleList.size()/2 - 1] + angleList[angleList.size()/2]) / 2;
                   else
                           median = angleList[angleList.size()/2];
                  */
  size_t medianIndex = angleLCopy.size() / 2;
  nth_element(angleLCopy.begin(), angleLCopy.begin() + medianIndex, angleLCopy.end());
  median = angleLCopy[medianIndex];
  std::cout &lt;&lt; "new Median method " &lt;&lt; angleLCopy[medianIndex] &lt;&lt; std::endl;
  //std::cout &lt;&lt; "greatest " &lt;&lt; greatest &lt;&lt; "|| lowest "&lt;&lt; lowest &lt;&lt; std::endl;
  
  //std::cout &lt;&lt; "No of matches by shehel: " &lt;&lt; angleList[35] &lt;&lt; " size " &lt;&lt; symMatchCount &lt;&lt; std::endl;
  //std::cout &lt;&lt; "Median" &lt;&lt; median &lt;&lt; std::endl;
  //std::cout &lt;&lt; matches.size()&lt;&lt; std::endl;
  count = 0;
  for (auto i = matches.begin(); i != matches.end();) {
  
      //std::cout &lt;&lt; angleList.at(count)&lt;&lt; std::endl;
  
      //if (angle &gt; greatest) greatest = angle;
      //if (angle &lt; lowest) lowest = angle;
      point1 = point1List.at(count);
      point2 = point2List.at(count);
  
      deltaY = ((360 - point2.y) - (360 - point1.y));
      deltaX = ((point2.x + 480) - point1.x);
  
      angle = atan2(deltaY, deltaX) * 180 / PI;
      //angleList.push_back (angle);
      cout &lt;&lt; "Is it sorted? " &lt;&lt; angleList.at(count) &lt;&lt; endl;
  
      if (angleList.at(count) &gt; (median + 5) | angleList.at(count) &lt; (median - 5)) {
          //cout &lt;&lt; "bitch is gone" &lt;&lt; angleList.at(count) &lt;&lt; endl;
          matches.erase(i);
          count++;
  
      }
          //{i++; count++;}
      else {
          cout &lt;&lt; "Points A (" &lt;&lt; point1.x &lt;&lt; ", " &lt;&lt; point1.y &lt;&lt; ") B (" &lt;&lt;
               point2.x + 480 &lt;&lt; ", " &lt;&lt; point2.y &lt;&lt; ") Deltas of X" &lt;&lt; deltaX &lt;&lt; " Y " &lt;&lt;
               deltaY &lt;&lt; "  Angle " &lt;&lt; angle &lt;&lt; endl;
          cout &lt;&lt; "aint going no where" &lt;&lt; angleList.at(count) &lt;&lt; endl;
  
          ++i;
          count++;
          //if (angle&gt;0.5 | angle &lt; -0.7)
          //matches.erase(matches.begin()+i);
          // do something with the best points...
      }
  }
  
  return (static_cast&lt;double&gt;(matches.size()) / static_cast&lt;double&gt;(matches1.size()));
  }
  </code></pre>