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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With