1

The stackoverflow.com/questions/5859673 states that the ReleaseStringUTFChars() must be called regardless the string was copied or not. So what is the parameter jboolean *isCopy in the GetStringUTFChars() good for?

Can I release the C string when the original jstring is out of scope? For example in this pseudo code:

static const char *cstr;

JNIEXPORT void JNICALL Java_com_Run(JNIEnv *e, jobject obj, jstring s) {
    cstr = (*e)->GetStringUTFChars(e, s, 0);
}

void cfunc() {
    // Can I use and release the cstr here? How?
}

According to documentation I have to get a reference via NewGlobalRef() and release it later if I want to use java object after the JNI call finish. Is this rule valid for strings retrieved via GetStringUTFChars()?

Community
  • 1
  • 1
Zaboj Campula
  • 3,155
  • 3
  • 24
  • 42

1 Answers1

1

isCopy parameter for GetStringUTFChars() is only an attempt to provide a signature consistent with other similar JNI functions. In all known JVM implementations it is always invariably JNI_TRUE, because the Java strings are stored internally in an incompatible UCS-16 format.

But still, you cannot release cstr without knowing the context.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307