22

I'm using Opencv sdk for Android to develop a real time processing and matching.

The main Opencv traitment logic is in a JNI function.

The problem is that sometimes (just sometimes) my app crashes without error, so I ignored the problem until I'm done developing the algorithm.

I started investigating the error and it's definetly in the JNI part.

Here is the error I get in the Log

A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 27424 (Thread-8)
A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0

I searched a lot in the internet and I found this solution

<activity
   android:hardwareAccelerated="false" />

It worked for 2 days and now I'm getting the same error back.

What's the problem and how can I solve it?

Any help would be appreciated and thank you in advance.

EDIT

I should add that my application take a reference image from gallery and compare it using Opencv with a real time image feed.

If I choose an image from gallery and the app crashes, that image won't work again and if I take a new image or an image that worked before, the app works fine.

Community
  • 1
  • 1
Amine
  • 2,241
  • 2
  • 19
  • 41
  • What do you mean `that image won't work again`? – c4pQ Apr 24 '19 at 08:15
  • I meant if I choose that image from my reference images, I get the error of segmentation fault – Amine Apr 24 '19 at 09:31
  • Is there any chance you're trying to modify this image? e.g. you're writing to this read_only image by mistake – c4pQ Apr 24 '19 at 09:40
  • No i'm just segmenting it and passing an array of 3 mats to the next activity for real time processing. – Amine Apr 24 '19 at 09:50
  • An other observation I realised is if I do the processing for more than 5 frames (that means 5 times) the application crashes – Amine Apr 24 '19 at 09:51
  • Do you free the memory after each use if it's allocated? Try to avoid raw memory manipulation. – c4pQ Apr 24 '19 at 09:56
  • I had this problem with memory as it gets to 1.2G after 3 or 4 frames , so I free memory using those lines `System.runFinalization(); Runtime.getRuntime().gc(); System.gc();` – Amine Apr 24 '19 at 09:58
  • And the segmentation fault problem is there before I free memory usage. – Amine Apr 24 '19 at 09:59
  • Khm. You do free memory, but you do it in Java code. SEGFAULT you receive in ndk. That means you have to clean the memory in ndk code, not in Java. – c4pQ Apr 24 '19 at 10:03
  • Do you mean i clear memory in the JNI method ? – Amine Apr 24 '19 at 10:06
  • Please post a complete answer with an example and I will be happy to accept it :) – Amine Apr 24 '19 at 10:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192303/discussion-between-user1056837-and-amine). – c4pQ Apr 24 '19 at 10:07

11 Answers11

19

I solved by

  1. Restarted my smartphone (physical device).

  2. If you are using Android Studio, then go to file --> Invalidate Cache & Restart.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Aditya Aggarwal
  • 610
  • 6
  • 7
7

If you are on android emulator try to [Cold Boot] it

snersesyan
  • 1,647
  • 17
  • 26
  • Can confirm a Power off and Power on of the emulated device does the trick—just a Restart did not. – Bink Jun 28 '23 at 16:07
4

After some discussion it became clear that the problem was with interaction with memory:

extern "C" 
jdouble 
JNICALL Java_com_foo(JNIEnv *env, jclass type, jlong addrRgba, jlong addrGray) { 
  Mat &mRgb = *(Mat *) addrRgba;
  Mat &mGray = *(Mat *) addrGray;

  return (jdouble) toGray(mRgb, mGray);
}

As a quick fix double toGray(Mat& rgb, Mat& gray); had to be changed to double toGray(Mat rgb, Mat gray)

Additional information can be found on topic CvMat deep copy

c4pQ
  • 884
  • 8
  • 28
  • Yes it fixed the problem for new taken images, in addition to that the process become slower but it's acceptable delay. Thank you mate :) – Amine Apr 24 '19 at 13:08
  • @Amine I cant seem to find that source code, Please advise im using openCVLibrary410? Thanks – Spurdow Feb 09 '22 at 05:38
4

In my case, I narrowed the problem down to loading an Admob ad in a fragment. but only when the screen rendered for the second time.

Example: I leave the screen, then I return back to the screen, via FragmentManager.

In any case, if it is adview, you can fix by turning off hardware acceleration in the AndroidManifest.xml, or even better, directly on the ad component itself:

val adRequest: AdRequest = AdRequest.Builder().build()
    // disable hardware acceleration on this view, as it was causing the screen to crash on reload.
    adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
    adView.loadAd(adRequest)

It was a bit tricky narrowing it down to this, but I noticed this:

I/DynamiteModule: Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:204204100

right above the message

" Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 19705 (RenderThread), pid 19631"

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • This is what exactly happening on my case! However it is hard to replicate the issue. Is this for banner ads only or native ads as well? – Bitwise DEVS Jul 06 '22 at 17:46
4

The easiest way to fix this for me was to uninstall and reinstall the app.

3

This problem did occur to me, but the verified answer didn't help. Instead, the combination of the other answers in this thread did. In a nutshell, all I did was:

  1. Invalidating Android Studio's cache.

  2. Wiping the device's data and cold booting it.

Vu Tuan Hung
  • 167
  • 1
  • 4
0

In my case, I was using Macbook Pro with iPad as external screen (sidecar mode), when I started getting this on the emulator.

After disconnecting sidecar. Its working fine.

Rishabh876
  • 3,010
  • 2
  • 20
  • 37
0

In my case, an exception was caught when I tried to listen to imageReader.acquireLatestImage() {image -> //do smth} when that reader was already closed by imageReader.close(). This was because I had several imageReaders, and at least one of them didn't have time to give out a new image before it was closed, and there was a small gap in time between the close and the callback invocation.

soulflavacrew
  • 1,456
  • 1
  • 6
  • 3
0

Try to invalidate Android Studio Cache from File -> Invalidate Caches / Restart

Also, check if there are no other critical errors (you can see them in Logcat after the app crashed).

0

Solution: Ensure that you have one instance on openCV working on the data. Add a flag to check if there is an other process working with an image.

Cause: This is results from having multiple uncontrolled number of openCV instances. Same applies if you are working with tflite

0

I tried disabling hermes in build.gradle and it worked.

pravchuk
  • 903
  • 10
  • 14