title: Getting error "attempting to detach while still running code" when calling JavaVm->DetachCurrentThread
body: <p>Getting error while calling (*JavaVM)->DetachCurrentThread(*JavaVM). I tried copy paste from google jni example but still unable to figure out how to solve the problem. </p>
  
  <pre><code>jint JNI_OnLoad(JavaVM *vm, void *reserved) {
          javaVM = vm;
          JNIEnv *env;
          if ((*vm)-&gt;GetEnv(vm, (void **) &amp;env, JNI_VERSION_1_6) != JNI_OK) {
              return JNI_ERR;
          }
          return JNI_VERSION_1_6;
      }
  
      void *start_download( ) {
          JavaVM *jVM = javaVM;
          JNIEnv *env;
          jint res = (*jVM)-&gt;GetEnv(jVM, (void**)&amp;env, JNI_VERSION_1_6);
          if (res != JNI_OK) {
              res = (*jVM)-&gt;AttachCurrentThread(jVM, &amp;env, NULL);
              if (JNI_OK != res) {
                  LOGD("Failed to AttachCurrentThread, ErrorCode = %d", res);
                  return NULL;
              }
          }
          (*jVM)-&gt;DetachCurrentThread(jVM);
          return NULL;
      }
  
  
      void download_in_thread(const char *url, const char *path) {
          int thread_id;
          int a = 1;
          pthread_t p_thread;
          pthread_attr_t threadAttr_;
  
          pthread_attr_init(&amp;threadAttr_);
          pthread_attr_setdetachstate(&amp;threadAttr_, PTHREAD_CREATE_DETACHED);
  
          thread_id = pthread_create(&amp;p_thread, &amp;threadAttr_, start_download(url, path), (void *) &amp;a);
      }
  </code></pre>