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!

Difference between GetStringUTFRegion and GetStringUTFChars

843829Oct 28 2008 — edited Oct 29 2008
Hello,

In my C++ to Java JNI app i need to get the value of a jstring and to keep it.

If i understood well there is two way to do this :
		const jstring j_value = (jstring) penv->GetObjectArrayElement(j_arrayOfValue, i);		
		jboolean isCopy;
		const char* str_value = penv->GetStringUTFChars(j_value, &isCopy);
...
		if (isCopy) {
			penv->ReleaseStringUTFChars(j_value, str_value);
		}	
or
		const jstring j_value = (jstring) penv->GetObjectArrayElement(j_arrayOfValue, i);
		int length = penv->GetStringUTFLength(j_value);		
		char* str_value = new char[length]; // ALLOC
		penv->GetStringUTFRegion(j_value,0,length,str_value);
Since i want to keep a copy of the jstring value the second method seems more appropriate (no need for extra memcpy).
However sometimes it seems that the second method makes my app crash during the deallocation of the char buffer. Is there something special to know about GetStringUTFRegion ? Does anybody got any problems with it ?

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 26 2008
Added on Oct 28 2008
7 comments
1,899 views