chapter5 Local and Global References

local references, global  references, and weak global references.
5.1 Local and Global References
5.1.1 Local References
注意:不要将JNI的Local 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.
两种方式invalidate localRef:
(1)虚拟机自动释放(函数return)
(2)DeleteLocalRef
5.1.2 Global References
Unlike local references, which are created by most JNI functions, global references are created by just one JNI function, NewGlobalRef.
/* 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);
    }
    ...
}
5.1.3 Weak Global References
They are created using  NewGlobalWeakRef and freed using DeleteGlobalWeakRef.
全局引用保证对象不被垃圾回收,弱全局引用不会保证对象不被垃圾回收.
Caching the class in a weak global reference allows  the mypkg.MyCls2 class to still be unloaded:
JNIEXPORT void JNICALL Java_mypkg_MyCls_f(JNIEnv *env, jobject self)
{
    static jclass myCls2 = NULL;
    if (myCls2 == NULL) {
        jclass myCls2Local =
        (*env)->FindClass(env, "mypkg/MyCls2");
        if (myCls2Local == NULL) {
               return; /* can’t find class */
        }
        myCls2 = NewWeakGlobalRef(env, myCls2Local);
        if (myCls2 == NULL) {
             return; /* out of memory */
        }
    }
    ... /* use myCls2 */
}
We would have to check whether the cached weak reference still points to a live class object or points to a class object that has already been garbage collected. The next section will explain how to perform such checks on weak global references.
5.1.4 Comparing References
(*env)->IsSameObject(env, obj1, obj2)
The rules for weak global references are somewhat different.
检查一个弱全局引用是否还指向一个Object:
You can use IsSameObject to determine whether a non-NULL weak global reference still points to a live object. Suppose wobj is a non-NULL weak global reference. The following call:
(*env)->IsSameObject(env, wobj, NULL)
returns JNI_TRUE if wobj refers to an object that has already been collected, 
and returns JNI_FALSE if wobj still refers to a live object
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值