Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(JNI) is object returned by java code need DeleteLocalRef?

i have searched all over the world knowing that, we should DeleteLocalRef if it's created in JNI code
then, should i also delete it if the object is newed and returned by Java code? such as:

// in java code
public SomeObject funcInJavaCode() {
    return new SomeObject();
}
// in jni code
funcInJNI {
    jobject obj = env->CallObjectMethod(...);
    ...
    // do i have to delete the obj here???
    env->DeleteLocalRef(obj);
}

thanks

like image 693
ZSaberLv0 Avatar asked Sep 02 '13 07:09

ZSaberLv0


1 Answers

No. Local references are garbage collected when the native function returns to Java (when Java calls native) or when the calling thread is detached from the JVM (in native calls Java). You need explicit DeleteLocalRef only when you have a long lived native function (e.g., a main loop) or create a large number of transient objects in a loop.

like image 63
jop Avatar answered Nov 05 '22 18:11

jop