GetArrayLength jni中崩溃

时间:2018-06-27 04:41:09

标签: android debugging memory-leaks java-native-interface

我们在jni的 GetArrayLength 方法上遇到了很多崩溃。该错误在任何情况下都无法重现。调试 GetArrayLength 时,我无法正常工作。找出此问题的确切解决方案。此崩溃发生在所有Android版本中,但是将崩溃日志记录在Android 8中。

#00  pc 0000000000039190  /system/lib/libc.so (tgkill+12)
#01  pc 0000000000013b41  /system/lib/libc.so (pthread_kill+52)
#02  pc 000000000001475f  /system/lib/libc.so (raise+10)
#03  pc 0000000000010fdd  /system/lib/libc.so 
(__libc_android_abort+36)
#04  pc 000000000000f53c  /system/lib/libc.so (abort+4)
#05  pc 000000000021fc55  /system/lib/libart.so 
(_ZN3art7Runtime5AbortEv+160)
#06  pc 00000000000a841b  /system/lib/libart.so 
(_ZN3art10LogMessageD1Ev+1322)
#07  pc 000000000022ffb5  /system/lib/libart.so
(_ZN3art6Thread9DumpStateERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPKS0_i+1724)
  #08  pc 00000000002302f9  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+16)
  #09  pc 000000000023944d  /system/lib/libart.so (_ZN3art10ThreadList10DumpLockedERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+104)
  #10  pc 000000000021f9b1  /system/lib/libart.so (_ZN3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+236)
  #11  pc 000000000021fbfd  /system/lib/libart.so (_ZN3art7Runtime5AbortEv+72)
  #12  pc 00000000000a841b  /system/lib/libart.so (_ZN3art10LogMessageD1Ev+1322)
  #13  pc 00000000000b2231  /system/lib/libart.so (_ZN3artL8JniAbortEPKcS1_+1060)
  #14  pc 00000000000b2789  /system/lib/libart.so (_ZN3art9JniAbortFEPKcS1_z+60)
  #15  pc 00000000001c3843  /system/lib/libart.so (_ZN3art3JNI14GetArrayLengthEP7_JNIEnvP7_jarray+634)

以下是发生崩溃的代码段:

void report_gl_error(int code, gl_error **errors_hash, bool unrecoverable) {
    native_shim *shim = get_native_shim();
    jmethodID method = shim->env->GetMethodID(shim->type, "reportGlError", "(I)[I");
    jint error = code;
    jintArray gl_errors_arr = (jintArray)shim->env->CallObjectMethod(shim->instance, method, error);

    if (shim->env->ExceptionCheck()) {
        shim->env->ExceptionDescribe();
        shim->env->ExceptionClear();
        return;
    }

    int length = shim->env->GetArrayLength(gl_errors_arr);
    int *error_arr = (int*) shim->env->GetIntArrayElements(gl_errors_arr, 0);

    for (int i = 0; i < length; i++) {
        int err = (int) error_arr[i];
        gl_error *error_obj = (gl_error *)malloc(sizeof(gl_error));
        error_obj->error_code = err;
        HASH_ADD_INT(*errors_hash, error_code, error_obj);
    }
    shim->env->ReleaseIntArrayElements(gl_errors_arr, error_arr, 0);

    if (unrecoverable) {
        jmethodID method = shim->env->GetMethodID(shim->type, "logNativeError", "()V");
        shim->env->CallVoidMethod(shim->instance, method);
    }
    }

编辑: get_native_shim方法将基本上调用 get_env 方法

static JNIEnv* get_env() {
JNIEnv* env;
static_vm->AttachCurrentThread(&env, NULL);
nativereturn env;
}

完整代码可在此处查看:

core.c

native_shim.cpp

0 个答案:

没有答案