From 5965f8606348095afa40221753c727d3b03f73cc Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Wed, 9 Oct 2013 18:09:24 +0800 Subject: [PATCH 1/2] issues #2899:Fix application will crash when back to background on Android. --- cocos2dx/platform/android/jni/JniHelper.cpp | 11 ++++++++++- cocos2dx/platform/android/jni/JniHelper.h | 1 + cocos2dx/platform/android/nativeactivity.cpp | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cocos2dx/platform/android/jni/JniHelper.cpp b/cocos2dx/platform/android/jni/JniHelper.cpp index 852208afc31f..22a3807df24e 100644 --- a/cocos2dx/platform/android/jni/JniHelper.cpp +++ b/cocos2dx/platform/android/jni/JniHelper.cpp @@ -58,6 +58,8 @@ namespace cocos2d { jobject JniHelper::classloader = NULL; JNIEnv* JniHelper::env = NULL; + static pthread_key_t thread_key; + JavaVM* JniHelper::getJavaVM() { pthread_t thisthread = pthread_self(); LOGD("JniHelper::getJavaVM(), pthread_self() = %X", thisthread); @@ -72,6 +74,10 @@ namespace cocos2d { JniHelper::cacheEnv(javaVM); } + void JniHelper::detach_current_thread (void *env) { + _psJavaVM->DetachCurrentThread(); + } + bool JniHelper::cacheEnv(JavaVM* jvm) { JNIEnv* _env = NULL; // get jni environment @@ -89,7 +95,8 @@ namespace cocos2d { // TODO : If calling AttachCurrentThread() on a native thread // must call DetachCurrentThread() in future. // see: http://developer.android.com/guide/practices/design/jni.html - + + pthread_key_create (&thread_key, JniHelper::detach_current_thread); if (jvm->AttachCurrentThread(&_env, NULL) < 0) { LOGD("Failed to get the environment using AttachCurrentThread()"); @@ -99,6 +106,8 @@ namespace cocos2d { } else { // Success : Attached and obtained JNIEnv! JniHelper::env = _env; + if (pthread_getspecific(thread_key) == NULL) + pthread_setspecific(thread_key, _env); return true; } diff --git a/cocos2dx/platform/android/jni/JniHelper.h b/cocos2dx/platform/android/jni/JniHelper.h index 01405d955963..fe1bf3881498 100644 --- a/cocos2dx/platform/android/jni/JniHelper.h +++ b/cocos2dx/platform/android/jni/JniHelper.h @@ -60,6 +60,7 @@ class CC_DLL JniHelper static jobject classloader; private: + static void detach_current_thread (void *env); static bool cacheEnv(JavaVM* jvm); static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo, diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index c5d387433a32..aa0f8f8199a5 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -215,10 +215,10 @@ static cocos_dimensions engine_init_display(struct engine* engine) { /** * Invoke the dispatching of the next bunch of Runnables in the Java-Land */ +static bool initialized = false; static void dispatch_pending_runnables() { static cocos2d::JniMethodInfo info; - static bool initialized = false; - + if (!initialized) { initialized = cocos2d::JniHelper::getStaticMethodInfo( info, @@ -652,7 +652,7 @@ void android_main(struct android_app* state) { engine_term_display(&engine); memset(&engine, 0, sizeof(engine)); - + initialized = false; return; } } From c854febf269816effbd978f329d620bd3a541977 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 10 Oct 2013 11:30:42 +0800 Subject: [PATCH 2/2] rename some variable --- cocos2dx/platform/android/jni/JniHelper.cpp | 8 ++++---- cocos2dx/platform/android/nativeactivity.cpp | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cocos2dx/platform/android/jni/JniHelper.cpp b/cocos2dx/platform/android/jni/JniHelper.cpp index 22a3807df24e..ab3bfc38553c 100644 --- a/cocos2dx/platform/android/jni/JniHelper.cpp +++ b/cocos2dx/platform/android/jni/JniHelper.cpp @@ -58,7 +58,7 @@ namespace cocos2d { jobject JniHelper::classloader = NULL; JNIEnv* JniHelper::env = NULL; - static pthread_key_t thread_key; + static pthread_key_t s_threadKey; JavaVM* JniHelper::getJavaVM() { pthread_t thisthread = pthread_self(); @@ -96,7 +96,7 @@ namespace cocos2d { // must call DetachCurrentThread() in future. // see: http://developer.android.com/guide/practices/design/jni.html - pthread_key_create (&thread_key, JniHelper::detach_current_thread); + pthread_key_create (&s_threadKey, JniHelper::detach_current_thread); if (jvm->AttachCurrentThread(&_env, NULL) < 0) { LOGD("Failed to get the environment using AttachCurrentThread()"); @@ -106,8 +106,8 @@ namespace cocos2d { } else { // Success : Attached and obtained JNIEnv! JniHelper::env = _env; - if (pthread_getspecific(thread_key) == NULL) - pthread_setspecific(thread_key, _env); + if (pthread_getspecific(s_threadKey) == NULL) + pthread_setspecific(s_threadKey, _env); return true; } diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index aa0f8f8199a5..b591953fb89d 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -215,19 +215,19 @@ static cocos_dimensions engine_init_display(struct engine* engine) { /** * Invoke the dispatching of the next bunch of Runnables in the Java-Land */ -static bool initialized = false; +static bool s_methodInitialized = false; static void dispatch_pending_runnables() { static cocos2d::JniMethodInfo info; - if (!initialized) { - initialized = cocos2d::JniHelper::getStaticMethodInfo( + if (!s_methodInitialized) { + s_methodInitialized = cocos2d::JniHelper::getStaticMethodInfo( info, "org/cocos2dx/lib/Cocos2dxHelper", "dispatchPendingRunnables", "()V" ); - if (!initialized) { + if (!s_methodInitialized) { LOGW("Unable to dispatch pending Runnables!"); return; } @@ -652,7 +652,7 @@ void android_main(struct android_app* state) { engine_term_display(&engine); memset(&engine, 0, sizeof(engine)); - initialized = false; + s_methodInitialized = false; return; } }