Reputation: 909
I wrote a function allocating Java array in Android JNI. However, if this function is called continuously from Java, an error[*Fatal signal 11 (SIGSEGV)] will occur.
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);
Should not I use this way? Or is there some measures?
Upvotes: 3
Views: 2446
Reputation: 12927
If you want to keep jobject (like jbyteArray) between JNI calls you need to make it a GlobalRef:
jbyteArray temp_buffer = env->NewByteArray(buflen);
buffer = (jbyteArray)env->NewGlobalRef(temp_buffer);
Only then remeber to delete object to free memory:
env->DeleteGlobalRef(buffer);
Upvotes: 4