About arrays allocated in JNI… here is a solution to the problem.
About arrays allocated in JNI
I wrote a function in Android JNI that allocates a Java array. However, if this function is called continuously from Java, an error [*Fatal signal 11 (SIGSEGV)] occurs.
C++
static jbyteArray buffer = NULL;
static int cbuflen = 0;
jbyteArray Java_com_sample_buffer_Buffer_updateBuffer(JNIEnv* env, jobject thiz, jlong handle, jint buflen)
{
if(buflen > cbuflen){
if(buffer != NULL) env->DeleteLocalRef(buffer);
buffer = env->NewByteArray(buflen);
cbuflen = buflen;
}
return buffer;
}
Java
byte[] buf = conv.updateBuffer(buflen);
Shouldn’t I use this way? Or what are the measures?
Solution
If you want to keep jobjects (such as jbyteArray) between JNI calls, you need to set it to GlobalRef:
jbyteArray temp_buffer = env->NewByteArray(buflen);
buffer = (jbyteArray)env->NewGlobalRef(temp_buffer);
Then remember to delete the object to free up memory:
env->DeleteGlobalRef(buffer);