Release Reference Types

I have to release all non primitive types resource such as jstring, jobject and jclass.

  • jclass
jobject createOuterInstance(JNIEnv *env) {
    jclass cls = env->FindClass("com/practice/Outer");

    ...

    jobject result = env->NewObject(cls, constructId);
    env->DeleteLocalRef(cls);
    return result;
}
  • jobject
jobject innerObj = createInnerInstance(env, innerCls, obj);
if (innerObj == nullptr) {
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "can not create instance of inner class.");
}

 __android_log_print(ANDROID_LOG_DEBUG, TAG, "value in inner class is %d", getBar(env, innerCls, innerObj));
env->DeleteLocalRef(innerObj);

JNI Types and Data Structures @Oracle Java SE document

Local reference table overflow

jstring createString(JNIEnv *env) {
   return env->NewStringUTF("A"); // create local reference and connect to the Java String object
}

extern "C" JNIEXPORT void JNICALL
Java_com_practice_NativeCallJavaTest_test(JNIEnv *env, jobject) {
  __android_log_print(ANDROID_LOG_DEBUG, TAG, "test()");

  int i = 0;
  for (; i < 10000; i++) {
    createString(env);
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "%d", i);
  }
}

The local reference will auto release after switch to Java layer, but before that I will get overflow error.

D/counter: 503
A/art: art/runtime/barrier.cc:90] Check failed: count_ == 0 (count_=-1, 0=0) Attempted to destroy barrier with non zero count
A/art: art/runtime/runtime.cc:366] Runtime aborting --- recursively, so no thread-specific detail!
A/art: art/runtime/runtime.cc:366] 

    --------- beginning of crash
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 1809 (com.hsy.anative)
A/DEBUG: Build fingerprint: 'Android/vbox86p/vbox86p:6.0/MRA58K/233:userdebug/test-keys'
A/DEBUG: Revision: '0'
A/DEBUG: ABI: 'x86'
A/DEBUG: pid: 1809, tid: 1809, name: com.hsy.anative  >>> com.hsy.anative <<<
A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
A/DEBUG: Abort message: 'art/runtime/indirect_reference_table.cc:115] JNI ERROR (app bug): local reference table overflow (max=512)'
A/DEBUG:     eax 00000000  ebx 00000711  ecx 00000711  edx 00000006
A/DEBUG:     esi f770fc50  edi 00000002
A/DEBUG:     xcs 00000023  xds 0000002b  xes 0000002b  xfs 00000007  xss 0000002b
A/DEBUG:     eip f72d46c6  ebp 00000711  esp ff844180  flags 00200206
A/DEBUG: Tombstone written to: /data/tombstones/tombstone_06
E/DEBUG: AM write failed: Broken pipe

Debugging Native Memory Use

results matching ""

    No results matching ""