java java原生接口调用DeleteLocalRef
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16619445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling DeleteLocalRef in java native interface
提问by ssk
I'm returning a jstring from a JNI method. I delete the local reference to it before returning the value.
我从 JNI 方法返回一个 jstring。我在返回值之前删除了对它的本地引用。
JNIEXPORT jstring JNICALL TestJNIMethod( JNIEnv* env, jclass )
{
jstring test_string = env->NewStringUTF( "test_string_value" );
env->DeleteLocalRef( test_string );
return test_string;
}
Would the calling JAVA method be still able to access the returned jstring or would the Garbage collector cleanup the memory?
调用 JAVA 方法是否仍然能够访问返回的 jstring 或者垃圾收集器是否会清理内存?
回答by Eoin
No it will not, however your code will work on Android versions earlier than ICS. From ICS on this code will correctly fail.
不,不会,但是您的代码可以在早于 ICS 的 Android 版本上运行。从 ICS 上这段代码将正确失败。
In general you don't need to delete local references yourself, once the JNI function returns to Java the references will get GC'd.
一般来说,您不需要自己删除本地引用,一旦 JNI 函数返回到 Java,这些引用就会被 GC 处理。
The exception to that rule is if you create a lot of them, perhaps in a loop. Then it is possible to fill the local reference table. See IBM: Overview of JNI object references.
该规则的例外情况是,如果您创建了很多它们,可能是在循环中。那么就可以填充本地参考表。请参阅IBM:JNI 对象引用概述。
You should read JNI Local Reference Changes in ICS. Even if you are not writing for Android, it still identifies many common mistakes.
您应该阅读ICS 中的 JNI 本地参考更改。即使您不是为 Android 编写代码,它仍会识别出许多常见错误。