(JNI) - object returned by java code, need DeleteLocalRef?

I searched all over the world, knowing that we need DeleteLocalRef if it is created in JNI code

then should I also delete it if the object appeared and was returned by Java code? eg:

// 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

+4
source share
2 answers

Not. Local references are garbage collection when a native function returns to Java (when Java calls native) or when the calling thread disconnects from the JVM (in natural Java calls). You need to explicitly DeleteLocalRef only if you have a long-lived native function (for example, the main loop) or create a large number of transition objects in the loop.

+2
source

You definitely can NOT remove the local ref for the returned object, as this call will release the reference to the object. for instance

  jbitmap = invokeObjectJavaMethod("MFImageToNative", "([B)Landroid/graphics/Bitmap;", byte_array); env->DeleteLocalRef(jbitmap); return jbitmap; 

if it fails, I believe that the consumer of the method is responsible for releasing the link. Some soul could give me an explanation of how to do this, I would be very grateful.

0
source

Source: https://habr.com/ru/post/1500088/


All Articles