问题描述
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 是否有是否返回副本.
我认为这是 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!