Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)' when writing into jfloat array

I'm currently implementing an android function with the ndk. I declared the function in java like

public static native void calculate(float[] rgb,float factor);

Then, I wrote the C function:

JNIEXPORT void JNICALL Java_<package>_calculate(
       JNIEnv * env,
       jobject object,
       jfloatArray rgbObject,
       jfloat factor){
   jfloat* rgb = (*env)->GetFloatArrayElements(env,rgbObject,0);
   if(rgb==NULL) return;

   rgb[0]=5; // Test, crash!

   (*env)->ReleaseFloatArrayElements(env,rgb,rgbObject,0);
   }

However, everytime I try to call the function with an array and some value I get the logcat message:

A/libc(16064): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)

after that, the app crashes. I figured out that it always occurs when writing into the float values. Reading doesn't lead to a crash.

Do I something wrong? Isn't it possible to write the array values?

like image 295
TSGames Avatar asked Oct 06 '22 12:10

TSGames


1 Answers

I found the solution! For some reason, the function was called with a null-value as an array. However, the rgb==NULL check didn't fired (I don't know for which reason). I fixed the problem by adding a second check before getting the values

JNIEXPORT void JNICALL Java_<package>_calculate(
       JNIEnv * env,
       jobject object,
       jfloatArray rgbObject,
       jfloat factor){
   if(rgbObject==NULL) return; // Check if incomming array is NULL-Pointer
   jfloat* rgb = (*env)->GetFloatArrayElements(env,rgbObject,0);
   if(rgb==NULL) return;

   rgb[0]=5; // Test, crash!

   (*env)->ReleaseFloatArrayElements(env,rgb,rgbObject,0);
   }
like image 185
TSGames Avatar answered Oct 09 '22 00:10

TSGames