Crash while calling ReleaseStringUTFChars for NewStringUTF string

N

Naresh Agarwal

Hi

I am writing a JNI application to call a C++ code from Java.

In JNI code, I'm creating a Java String from char* using NewStringUTF
and then calling ReleaseStringUTFChars on this string.

However, my program crashes on ReleaseStringUTFChars.

Should we call ReleaseStringUTFChars on strings created using
NewStringUTF to indicate JVM to garbage collect these strings?

Code is as follows:

----------
Java Class
----------

public class Test {

public void getStringBuffer(StringBuffer sb)
{
return _getStringBuffer(sb);
}
private native _getStringBuffer(StringBuffer sb)
}

--------
JNI Code
--------

JNIEXPORT jint JNICALL Java_Test_getStringBuffer
(JNIEnv *env, jobject, jobject sb)
{

const char *data = //get a C++ api to get data

// create a Java String with this data
jstring strData = env->NewStringUTF(data);

// Call "append" method of StringBuffer
// append_mid is method id for "append"
env->CallObjectMethod(sb, append_mid, strData);

env->ReleaseStringUTFChars(strData, data); // CODE CRASHES HERE
}


thanks & regards,
Naresh
 
C

Chris Uppal

Naresh said:
Should we call ReleaseStringUTFChars on strings created using
NewStringUTF to indicate JVM to garbage collect these strings?

No, when you create a java.lang.String using NewStringUTF() the JVM takes a
copy of the data you supply, so you should not, indeed you /must/ not, use
ReleaseStringUTFChars().

On the other hand, if the input data was allocated using C malloc() (or its
friends) or a C++ new() then you will have to remember to free()/delete() the
data after Java has made a copy of it.

-- chris
 
N

Naresh Agarwal

I think I didn't make my self clear. Let me rephrase the question -

Should(Can) we call ReleaseStringUTFChars on strings created using
NewStringUTF after using these strings (i.e., once we are sure that we
don't need the string anymore) to indicate JVM to garbage collect these
strings.

For example, in JNI code below, we are following the steps given below
-

1. Create a jstring from char* data using NewStringUTF
2. Use this jstring to call "append" function of StringBuffer
3. Release the jstring using ReleaseStringUTFChars

--------
JNI Code
--------

JNIEXPORT jint JNICALL Java_Test_getStringBuffer
(JNIEnv *env, jobject, jobject sb /* stringbuffer object */)
{
const char *data = //get a C++ api to get data

// create a Java String with this data
jstring strData = env->NewStringUTF(data);

// Call "append" method of StringBuffer
// append_mid is method id for "append"
env->CallObjectMethod(sb, append_mid, strData);

env->ReleaseStringUTFChars(str­Data, data); // CODE CRASHES HERE
}

thanks,
Naresh
 
G

Gordon Beaton

Should(Can) we call ReleaseStringUTFChars on strings created using
NewStringUTF after using these strings (i.e., once we are sure that
we don't need the string anymore) to indicate JVM to garbage collect
these strings.

No. Use ReleaseStringUTFChars() only after GetStringUTFChars().

Your strings created with NewStringUTF() will be garbage collected
when you return from the native method (earlier if you use
PopLocalFrame() or DeleteLocalRef(), but in most cases returning from
the method is sufficient).

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top