Should I free the allocated string after passing it to NewStringUTF()
?
I have some code similar to:
char* test;
jstring j_test;
test = some_function(); // <- malloc()s the memory
j_test = (*env)->NewStringUTF(env, test);
free(test); // <- should this be here?
When I free the string after passing it to NewStringUTF()
, I get a signal 11 (SIGSEGV), fault addr deadbaad
error. If I remove the free()
call, the error vanishes. What am I doing wrong?
I see conflicting opinions. Some say I should free it myself, some say the VM frees it, some say the VM doesn't free it and you should do strange voodoo magic to free it. I'm confused.
See Question&Answers more detail:os