Memory allocation for a jstring!
843829May 1 2002 — edited May 2 2002Hello people,
I have a simple question. Well, I have a native method that returns a jstring. Here is the method:
extern "C" JNIEXPORT jstring JNICALL
Java_MyClass_createFoo(JNIEnv* env, jobject obj, jstring jFoo)
{
const char* foo=env->GetStringUTFChars(jFoo,0);
const char* result;
//Calling other C++ function, which gets as parameter the const char* result variable.
bool fooCreated = createFooImpl(foo, &result);
//Releasing the foo char*.
env->ReleaseStringUTFChars(jFoo, foo);
jstring jResult;
if(fooCreated)
{
//Return of the result.
jResult = env->NewStringUTF(result);
delete const_cast<char*>(result);
return jResult;
}
else
{
jResult = env->NewStringUTF("Error message");
return jResult;
}
}
So the question is, who is responsible for releasing the memory allocated for jResult? Is it true that after
return jResult;
the memory is automatically released or I have to release it by myself somehow?
You can see that I use
delete const_char<char*>(result*)
in order to release the memory allocated for result* .