[JNI]ローカル参照は静的変数に格納できません。グローバル参照は静的変数に格納できます

次のコードの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)を返します。最初のCf()メソッドが返された後、仮想マシンは、stringClassを含むCf()中に作成されたすべてのlocalRefを解放します。

ただし、stringClass!= NULLの場合、Cf()が2回呼び出されると、MyNewString()はFindClassを呼び出さなくなり、新しいlocalRefを生成しません。現時点では、stringClassは無効なlocalRefであるため、システムがクラッシュする可能性があります。 。

 

グローバル参照は静的変数に格納できます。

/* 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] Java™NativeInterfaceプログラマーズガイドおよびSpecification.pdf

 

 

おすすめ

転載: blog.csdn.net/u012906122/article/details/103700508