如果GetStringUTFChars返回副本,你应该调用ReleaseStringUTFChars吗? [英] Should you call ReleaseStringUTFChars if GetStringUTFChars returned a copy?

查看:285
本文介绍了如果GetStringUTFChars返回副本,你应该调用ReleaseStringUTFChars吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rob Gordon的Essential JNI:Java Native Interface一书包含以下代码示例,用于将jstring转换为C字符串:

The book "Essential JNI: Java Native Interface" by Rob Gordon contains the following code example to convert a jstring to a C string:

const char* utf_string;
jboolean isCopy;
utf_string = env->GetStringUTFChars(str, &isCopy);
/* ... use string ... */
if (isCopy == JNI_TRUE) {
    env->ReleaseStringUTFChars(str, utf_string);
}

请注意,它只调用 ReleaseStringUTFChars 如果 isCopy 为真。

Note that it only calls ReleaseStringUTFChars if isCopy is true.

但书 Java Native Interface:程序员指南和规范(备用链接: http://192.9.162.55/docs/books/jni/html/objtypes.html#5161 )说:

But the book Java Native Interface: Programmer's Guide and Specification (alternate link: http://192.9.162.55/docs/books/jni/html/objtypes.html#5161) says:


无论GetStringChars是否将
设置为* isCopy,ReleaseString-Chars调用都是
必需的JNI_TRUE或JNI_FALSE。
ReleaseStringChars释放
副本或取消实例,取决于
,取决于GetStringChars是否有
返回副本。

The ReleaseString-Chars call is necessary whether GetStringChars has set *isCopy to JNI_TRUE or JNI_FALSE. ReleaseStringChars either frees the copy or unpins the instance, depending upon whether GetStringChars has returned a copy or not.

我认为这是戈登书中的错误是正确的吗?

I am correct in assuming this is a bug in Gordon's book?

推荐答案

是的,你的假设是正确的(你应该总是调用ReleaseStringUTFChars)。

Yes, your assumption is correct (you should always call ReleaseStringUTFChars).

这篇关于如果GetStringUTFChars返回副本,你应该调用ReleaseStringUTFChars吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆