What is the exactly time that JNI release the LocalReference automatically?

29 Views Asked by At

Think about this situation in jni:

  1. I first call a native function1 in java
  2. The native function1 calls the java function2
  3. The java function2 calls the native function3

What I need to know is when will the LocalReference created in function3 be automatically released. Is it the time the function3 returns or the time the function1 returns?

I tried to ask gpt4 and claude3. The answer of gpt4 is the time function1 returns. The answer of claude3 is the time function3 returns.

1

There are 1 best solutions below

0
marcinj On

What I need to know is when will the LocalReference created in function3 be automatically released.

the answer is when the native call will return to java, so when function3 returns back to function2 in java. Local references are valid for the duration of a native method call. They are freed automatically after the native method returns.

For more android related reference :

https://developer.android.com/training/articles/perf-jni#local-and-global-references

Every argument passed to a native method, and almost every object returned by a JNI function is a "local reference". This means that it's valid for the duration of the current native method in the current thread. Even if the object itself continues to live on after the native method returns, the reference is not valid.