Zaboj Campula
Zaboj Campula

Reputation: 3360

How GetStringUTFChars and ReleaseStringUTFChars works?

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()?

Upvotes: 1

Views: 1635

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57183

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.

Upvotes: 1

Related Questions