Call DeleteLocalRef in native Java interface

I am returning jstring from the JNI method. I delete the local link before returning the value.

JNIEXPORT jstring JNICALL TestJNIMethod( JNIEnv* env, jclass ) { jstring test_string = env->NewStringUTF( "test_string_value" ); env->DeleteLocalRef( test_string ); return test_string; } 

Can the calling JAVA method still have access to the returned jstring string or will the garbage collector clear the memory?

+7
source share
1 answer

No, this will not happen, however your code will work on versions of Android earlier than ICS. From ICS, this code will complete correctly.

In general, you do not need to delete local links yourself, as soon as the JNI function returns to Java, the links will get GC'd.

An exception to this rule is if you create a lot of them, possibly in a loop. You can then populate the local lookup table. See IBM: An Overview of JNI Object Links .

You should read the JNI Local Reference Changes on ICS . Even if you are not writing for Android, it still identifies many common errors.

+18
source

All Articles