title: Crashes in GetArrayLength jni
body: <p>We are getting lots of crashes at <strong>GetArrayLength</strong> method in jni.The bug is not reproducible at any situation.When debugging the <strong>GetArrayLength</strong> is working properly.I am not able to find out the exact solution for this issue.This crash is happening in all Android version,however putting log of the crash in Android 8.</p>
  
  <pre><code>#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)
  </code></pre>
  
  <p>Here's the snippet of the code where is crashed is happening:</p>
  
  <pre><code>void report_gl_error(int code, gl_error **errors_hash, bool unrecoverable) {
      native_shim *shim = get_native_shim();
      jmethodID method = shim-&gt;env-&gt;GetMethodID(shim-&gt;type, "reportGlError", "(I)[I");
      jint error = code;
      jintArray gl_errors_arr = (jintArray)shim-&gt;env-&gt;CallObjectMethod(shim-&gt;instance, method, error);
  
      if (shim-&gt;env-&gt;ExceptionCheck()) {
          shim-&gt;env-&gt;ExceptionDescribe();
          shim-&gt;env-&gt;ExceptionClear();
          return;
      }
  
      int length = shim-&gt;env-&gt;GetArrayLength(gl_errors_arr);
      int *error_arr = (int*) shim-&gt;env-&gt;GetIntArrayElements(gl_errors_arr, 0);
  
      for (int i = 0; i &lt; length; i++) {
          int err = (int) error_arr[i];
          gl_error *error_obj = (gl_error *)malloc(sizeof(gl_error));
          error_obj-&gt;error_code = err;
          HASH_ADD_INT(*errors_hash, error_code, error_obj);
      }
      shim-&gt;env-&gt;ReleaseIntArrayElements(gl_errors_arr, error_arr, 0);
  
      if (unrecoverable) {
          jmethodID method = shim-&gt;env-&gt;GetMethodID(shim-&gt;type, "logNativeError", "()V");
          shim-&gt;env-&gt;CallVoidMethod(shim-&gt;instance, method);
      }
      }
  </code></pre>
  
  <p>Edit:
  get_native_shim method will basically call <strong>get_env</strong> method</p>
  
  <pre><code>static JNIEnv* get_env() {
  JNIEnv* env;
  static_vm-&gt;AttachCurrentThread(&amp;env, NULL);
  nativereturn env;
  }
  </code></pre>
  
  <p>Full code can be viewed up here:</p>
  
  <p><a href="https://github.com/gameclosure/native-core/blob/9ed8d903d92ffa6e52af32e18540bbee8defed08/core.c" rel="nofollow noreferrer">core.c</a></p>
  
  <p><a href="https://github.com/gameclosure/native-android/blob/6ccfcaf54d073b31ad424e86b55e9df9af4ea72b/TeaLeaf/jni/platform/native_shim.cpp" rel="nofollow noreferrer">native_shim.cpp</a></p>