Should you call ReleaseStringUTFChars if GetStringUTFChars returned a copy?(如果 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果 GetStringUTFChars 返回了一个副本,你应该调用 ReleaseStringUTFChars 吗?
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
