So if we attach we must detach thread after after it finish, right?
JAVA_VM->GetEnv((void**) &res, JNI_VERSION_1_6);//Using cached JavaVM
JNIEnv* env = get_jni_env();
... //Here i delete global refs (jclass)
static void* thread_func(void* arg)
get_jni_env(); // attach new thread
//Do thread stuff
JAVA_VM->DetachCurrentThread();//thread done detached it with ok
Activity.onDestroy() method is called on the UI thread. Why are you trying to detach the Java VM from the UI thread? That thread is managed by the system, you should neither attach nor detach the Java VM from/to it.
JNIEnv* is available to every native method as the first parameter. Why do you need
get_jni_env() in the first place?
If you need a JNIEnv on worker threads, then you need to attach and detach (OR spawn a thread from Java; it's quite easy).
EDIT: If it's a repeat attachment, you don't need to detach. It's not a ref-counted system.
AttachCurrentThread is documented as
Trying to attach a thread that is already attached is a no-op.
As opposed to requiring matched attach/detach calls.