title: AttachCurrentThread crash in multi threads JNI after moving to Android L release
body: <p>I have implemented a service which is invoked by system server during the boot-up.</p>
  
  <p>My service has JNI implementation which creates another thread.
  The reason for having another thread is to have the capability of canceling operation while monitoring the call back.</p>
  
  <p>This mechanism worked well in Android Kitkat release but it crashes in Android L.</p>
  
  <p>Between two threads, I stored the JavaVM* that I get from GetJavaVM(env) to a static global variable. Of course, this shared data is protected by pthread mutex.</p>
  
  <p>I have tried the following so far, but all of them below still crashed :</p>
  
  <p>1) Used JavaVM* I get from JNI onLoad() function by store it to global</p>
  
  <p>2) In the new thread, as there is only one JavaVM running on Android, get the vm from calling  android::AndroidRuntime::getJavaVM();</p>
  
  <p>3) Stored the vm information in the main thread after calling NewGlobalRef(). And saved that reference to the shared data. The new thread used the reference from NewGlobalRef().</p>
  
  <p>Does anyone know what is significantly changed in JNI environment on Android L release? </p>
  
  <p>UPDATE :</p>
  
  <p>Debugged further and the solution I mentioned 1) or 2) should have worked.
  The actual issue was due to the garbage collection running more frequently. So the HAL pointer I kept was not valid any more...</p>
  
  <p>These links were helpful!!!</p>
  
  <p><a href="https://developer.android.com/guide/practices/verifying-apps-art.html" rel="nofollow">https://developer.android.com/guide/practices/verifying-apps-art.html</a></p>
  
  <p><a href="http://developer.android.com/training/articles/perf-jni.html" rel="nofollow">http://developer.android.com/training/articles/perf-jni.html</a></p>
  
  <p>Thanks for all the comments!</p>