Crash while calling ReleaseStringUTFChars for NewStringUTF string

Discussion in 'Java' started by Naresh Agarwal, Jul 1, 2005.

  1. 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
     
    Naresh Agarwal, Jul 1, 2005
    #1
    1. Advertising

  2. Naresh Agarwal

    Chris Uppal Guest

    Naresh Agarwal wrote:

    > 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
     
    Chris Uppal, Jul 1, 2005
    #2
    1. Advertising

  3. 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
     
    Naresh Agarwal, Jul 1, 2005
    #3
  4. On 1 Jul 2005 11:13:39 -0700, Naresh Agarwal wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jul 1, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lasse
    Replies:
    1
    Views:
    719
    Jon A. Cruz
    Jan 5, 2004
  2. Hicks
    Replies:
    1
    Views:
    1,000
    Roedy Green
    Jun 30, 2004
  3. qazmlp
    Replies:
    6
    Views:
    2,583
    Québec
    Jul 31, 2004
  4. Peter
    Replies:
    5
    Views:
    14,178
    Dale King
    Jun 2, 2006
  5. Marc
    Replies:
    0
    Views:
    362
Loading...

Share This Page