sunny ruan
sunny ruan

Reputation: 31

how to release char**

how to release char** in jni? I don't find any env function to release pstr below

Java_com_example_test(
JNIEnv* env,jobject,jobjectArray content){
 jsize len = env->GetArrayLength(content);
 char **pstr = (char**)malloc(len* sizeof(char*));
 jstring jstr;
 for (int i=0; i<len;i++){
     jstr = (jstring)env->GetObjectArrayElement(content,i);
     pstr[i]=(char*) env->GetStringUTFChars(jstr,0);
 }
 env->DeleteLocalRef(jstr);
 // todo release pstr

Upvotes: 0

Views: 254

Answers (1)

KPR
KPR

Reputation: 101

free(pstr);
pstr = NULL;

will free memory allocated with malloc, but before that you need to release memory allocated by VM to prevent memory leak. From Java SE doc

 const char * GetStringUTFChars(JNIEnv *env, jstring string,
jboolean *isCopy);

Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by ReleaseStringUTFChars().

Upvotes: 1

Related Questions