本文主要是介绍Local Reference不可存入到一个static变量里,Global Reference可存入一个static变量里,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来看下如下代码的stringClass:
/* This code is illegal */
jstring MyNewString(JNIEnv *env, jchar *chars, jint len)
{static jclass stringClass = NULL;jmethodID cid;jcharArray elemArr;jstring result;if (stringClass == NULL) {stringClass = (*env)->FindClass(env,"java/lang/String");if (stringClass == NULL) {return NULL; }}/* It is wrong to use the cached stringClass here, because it may be invalid. */cid = (*env)->GetMethodID(env, stringClass,"<init>", "([C)V");elemArr = (*env)->NewCharArray(env, len);result = (*env)->NewObject(env, stringClass, cid, elemArr);(*env)->DeleteLocalRef(env, elemArr);return result;
}JNIEXPORT jstring JNICALL Java_C_f(JNIEnv *env, jobject this)
{char *c_str = ...;...return MyNewString(c_str,len);
}//Java层调用两次Java_C_f接口:
...
... = C.f(); // The first call is perhaps OK.
... = C.f(); // This would use an invalid local reference.
...
解释:
FindClass返回了java.lang.String的localRef(stringClass),在第一次C.f()方法return后,虚拟机释放了C.f()期间创建的所有localRef,包括stringClass.
但stringClass!=NULL,第二次调用C.f()时,MyNewString()不会再调用FindClass,不会生成新的localRef.此时stringClass是无效的localRef,可能导致系统crash.
Global Reference可存入一个static变量里:
/* This code is OK */
jstring MyNewString(JNIEnv *env, jchar *chars, jint len)
{static jclass stringClass = NULL;...if (stringClass == NULL) {jclass localRefCls = (*env)->FindClass(env, "java/lang/String");if (localRefCls == NULL) {return NULL; /* exception thrown */}/* Create a global reference */stringClass = (*env)->NewGlobalRef(env, localRefCls);if (stringClass == NULL) {return NULL; /* out of memory exception thrown */}/* The local reference is no longer useful */(*env)->DeleteLocalRef(env, localRefCls);}...
}
参考文献:
[1] The Java™ Native Interface Programmer’s Guide and Specification.pdf
这篇关于Local Reference不可存入到一个static变量里,Global Reference可存入一个static变量里的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!