NDK app onDestroy cleanup - how to DetachCurrentThread

Posted by Aristarhys on Stack Overflow See other posts from Stack Overflow or by Aristarhys
Published on 2012-10-22T11:58:22Z Indexed on 2012/10/22 17:01 UTC
Read the original article Hit count: 193

Filed under:
|
|
|

So if we attach we must detach thread after after it finish, right?

JNIEnv* get_jni_env()
{
JNIEnv* res;
JAVA_VM->GetEnv((void**) &res, JNI_VERSION_1_6);//Using cached JavaVM
JAVA_VM->AttachCurrentThread(&res, NULL);
return res;
}

I call next native method from @Override protected void onDestroy() of my Activity class

 void free_jni_manager()
 {
   JNIEnv* env = get_jni_env();
   ... //Here i delete global refs (jclass)
  //JAVA_VM->DetachCurrentThread();
 }

ERROR: detaching thread with interp frames (count=16) - main thread still running and we try to detach it.

Even if we take any function that use JNIEnv (for example calling java methods), putting DetachCurrentThread will cause same error.

DetachCurrentThread works flawlessly if used in pthread function

static void* thread_func(void* arg)
{
 get_jni_env(); // attach new thread
 //Do thread stuff
 JAVA_VM->DetachCurrentThread();//thread done detached it with ok
 return NULL;
}

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.S. What benefits of using AttachCurrentThreadAsDaemon()

© Stack Overflow or respective owner

Related posts about android

Related posts about c++