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

查看:30
本文介绍了如果 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);
}

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

Note that it only calls ReleaseStringUTFChars if isCopy is true.

但是本书Java Native Interface: Programmer's Guide and Specification(替代链接: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:

ReleaseString-Chars 调用是GetStringChars 是否有必要将 *isCopy 设置为 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.

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

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天全站免登陆