我需要清理传递给NewStringUTF的char *吗? [英] Do I need to clean up the char* passed to NewStringUTF?
本文介绍了我需要清理传递给NewStringUTF的char *吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为是的,但是我发现的前12个示例都做了一些不具说明性的操作
I think yes, but the top 12 examples I found all do something not illustrative like
JNIEXPORT jstring JCALL Java_com_foo_dumbImpl(JNIEnv* env, jobject thisObj)
{
return (*env)->NewStringUTF(env, "constant string");
}
所以后代我会问:这不好,是吗?
so for posterity I will ask: this is bad, yes?
JNIEXPORT jstring JCALL Java_com_foo_dumbImpl(JNIEnv* env, jobject thisObj)
{
char *leak = malloc(1024);
leak[0] = '\0';
return (*env)->NewStringUTF(env, leak);
}
...并且应该是:
JNIEXPORT jstring JCALL Java_com_foo_dumbImpl(JNIEnv* env, jobject thisObj)
{
char *emptystring = NULL;
jstring r = NULL;
emptystring = malloc(1024);
emptystring[0] = '\0';
r = (*env)->NewStringUTF(env, emptystring);
free(emptystring);
emptystring = NULL;
return r;
}
推荐答案
是. (只是为了避免出现这种情况,所以没有答案.)
Yes. (Just so this doesn't look unanswered.)
这篇关于我需要清理传递给NewStringUTF的char *吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文