jni 出错 ReferenceTable overflow

一、多次NewByteArray后,报错“ReferenceTable overflow”

解决办法:释放所有对object的引用

例: jbyteArray audioArray = jnienv->NewByteArray(frameSize);
       jnienv->SetByteArrayRegion(audioArray,0,frameSize,(jbyte*)fReceiveBuffer);
       jnienv->DeleteLocalRef(audioArray);

1.FindClass 

例如,

jclass ref= (env)->FindClass("java/lang/String");

env->DeleteLocalRef(ref); 

 

2.NewString/ NewStringUTF/NewObject/NewByteArray

例如,

jstring     (*NewString)(JNIEnv*, const jchar*, jsize);    

const jchar* (*GetStringChars)(JNIEnv*, jstring, jboolean*);     

void        (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*);
jstring     (*NewStringUTF)(JNIEnv*, const char*);    

const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*);     

void        (*ReleaseStringUTFChars)(JNIEnv*, jstring, const char*);
env->DeleteLocalRef(ref);

 

3.GetObjectField/GetObjectClass/GetObjectArrayElement

jclass ref = env->GetObjectClass(robj);

env->DeleteLocalRef(ref); 

 

4.GetByteArrayElements

jbyte* array= (*env)->GetByteArrayElements(env,jarray,&isCopy);

(*env)->ReleaseByteArrayElements(env,jarray,array,0);

 

5.const char* input =(*env)->GetStringUTFChars(env,jinput, &isCopy);

(*env)->ReleaseStringUTFChars(env,jinput,input);

 

6.NewGlobalRef/DeleteGlobalRef

 jobject     (*NewGlobalRef)(JNIEnv*, jobject);     

void        (*DeleteGlobalRef)(JNIEnv*, jobject);

例如,

jobject ref= env->NewGlobalRef(customObj);

env->DeleteGlobalRef(customObj);
posted @ 2013-04-07 18:09  little_star  阅读(584)  评论(0编辑  收藏  举报