Look at the stringClass of the following code:
/* 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.
...
Explanation:
FindClass returns the localRef(stringClass) of java.lang.String. After the first Cf() method return, the virtual machine releases all localRefs created during Cf(), including stringClass.
But stringClass!=NULL, when Cf() is called for the second time, MyNewString() will no longer call FindClass and will not generate a new localRef. At this time, stringClass is an invalid localRef, which may cause system crash.
Global Reference can be stored in a static variable:
/* 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);
}
...
}
references:
[1] The Java™ Native Interface Programmer’s Guide and Specification.pdf