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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With