Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Memory allocation for a jstring!

843829May 1 2002 — edited May 2 2002
Hello 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* .
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2002
Added on May 1 2002
3 comments
239 views