Dowemo
0 0 0 0

1. Which requires manual release?

  • Don't release manually ( basic type ): Jint, jlong, jchar

  • You need to manually release ( reference type, array family ): Jstring, jobject, jobjectArray, jintArray, jclass, jmethodID

2. Release methods.

2. 1 jstring & amp; char *.

// 创建 jstring 和 char*
jstring jstr = (*jniEnv)->CallObjectMethod(jniEnv, mPerson, getName);
char* cstr = (char*)(*jniEnv)->GetStringUTFChars(jniEnv,jstr, 0);// 释放(*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, cstr);(*jniEnv)->DeleteLocalRef(jniEnv, jstr);

Reference types such as 2. 2 jobject, jobjectArray, jclass, jmethodID.

(*jniEnv)->DeleteLocalRef(jniEnv, XXX);

2. 3 jbyteArray.

jbyteArray audioArray = jnienv->NewByteArray(frameSize);jnienv->DeleteLocalRef(audioArray);

2. 4 GetByteArrayElements.

jbyte* array= (*env)->GetByteArrayElements(env,jarray,&isCopy);(*env)->ReleaseByteArrayElements(env,jarray,array,0);

2. 5 NewGlobalRef.

jobjectref= env->NewGlobalRef(customObj);env->DeleteGlobalRef(customObj);

3. Avoid memory leaks.

Jni can easily cause memory leaks if the above reference is created without manual release, so the reference type created by jni programming must be




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs