title: NDK app onDestroy cleanup - how to DetachCurrentThread
body: <p>So if we attach we must detach thread after after it finish, right?</p>
  
  <pre><code>JNIEnv* get_jni_env()
  {
  JNIEnv* res;
  JAVA_VM-&gt;GetEnv((void**) &amp;res, JNI_VERSION_1_6);//Using cached JavaVM
  JAVA_VM-&gt;AttachCurrentThread(&amp;res, NULL);
  return res;
  }
  </code></pre>
  
  <p>I call next native method from <strong>@Override protected void onDestroy()</strong> of my Activity class</p>
  
  <pre><code> void free_jni_manager()
   {
     JNIEnv* env = get_jni_env();
     ... //Here i delete global refs (jclass)
    //JAVA_VM-&gt;DetachCurrentThread();
   }
  </code></pre>
  
  <p><strong>ERROR: detaching thread with interp frames (count=16)</strong> -  main thread still running and we try to detach it.</p>
  
  <p>Even if we take any function that use JNIEnv (for example calling java methods), putting DetachCurrentThread will cause same error.</p>
  
  <p><strong>DetachCurrentThread</strong> works flawlessly if used in <strong>pthread function</strong></p>
  
  <pre><code>static void* thread_func(void* arg)
  {
   get_jni_env(); // attach new thread
   //Do thread stuff
   JAVA_VM-&gt;DetachCurrentThread();//thread done detached it with ok
   return NULL;
  }
  </code></pre>
  
  <p>Do we need detach main thread then we done with JNI, there do it? Or then activity will be destroyed, it will freed itself with JavaVM? Do we need do call DestroyJavaVM() (just doing crash if use onDestroy), how free cached JavaVM or garbage cleaner will handle this?</p>
  
  <p>P.S. What benefits of using <strong>AttachCurrentThreadAsDaemon()</strong></p>