From fe2e0c56a3e4239b56fb0dbe1c4d25b004df68dd Mon Sep 17 00:00:00 2001 From: Hasufel Date: Mon, 17 Feb 2014 22:00:27 +0100 Subject: [PATCH] Multithread was not properly detached ..causing crash in some cases returning 'native thread exited without detaching' error message. --- project/src/platform/android/AndroidCommon.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/project/src/platform/android/AndroidCommon.cpp b/project/src/platform/android/AndroidCommon.cpp index eb6de0c96..a383fcb0d 100644 --- a/project/src/platform/android/AndroidCommon.cpp +++ b/project/src/platform/android/AndroidCommon.cpp @@ -2,13 +2,20 @@ #include #include #include -#include +#include +#include #undef LOGE #define LOGE(msg,args...) __android_log_print(ANDROID_LOG_ERROR, "lime::System", msg, ## args) JavaVM *_vm; std::map jClassCache; +static pthread_key_t s_thread_key; + +static void ThreadDataDestroy (void *env) +{ + _vm->DetachCurrentThread(); +} JNIEnv *GetEnv() { @@ -19,6 +26,7 @@ JNIEnv *GetEnv() if (_vm->AttachCurrentThread(&env, 0) != 0) { LOGE("Failed to attach"); } + pthread_setspecific (s_thread_key, &env); } return env; } @@ -46,6 +54,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { _vm = vm; jClassCache = std::map(); + pthread_key_create(&s_thread_key, ThreadDataDestroy); return JNI_VERSION_1_4; // the required JNI version }