title: Can't execute JavaVM->DetachCurrentThread(): "attempting to detach while still running code"
body: <p>I have an Android app that uses NDK - a regular Android Java app with regular UI and C++ core. There are places in the core where I need to call Java methods, which means I need a <code>JNIEnv*</code> for that thread, which in turn means that I need to call <code>JavaVM-&gt;AttachCurrentThread()</code> to get the valid <code>env</code>. </p>
  
  <p>Previously, was just doing <code>AttachCurrentThread</code> and didn't bother to detach at all. It worked fine in Dalvik, but ART aborts the application as soon as a thread that has called <code>AttachCurrentThread</code> exits without calling <code>DetachCurrentThread</code>. So I've read the JNI reference, and indeed it says that I must call <code>DetachCurrentThread</code>. But when I do that, ART aborts the app with the following message:</p>
  
  <blockquote>
    <p>attempting to detach while still running code</p>
  </blockquote>
  
  <p>What's the problem here, and how to call <code>DetachCurrentThread</code> properly?</p>