Merge pull request #3903 from Dhilan007/di2899

issues #2899:Fix application will crash when back to background on Andro...
This commit is contained in:
James Chen 2013-10-09 23:38:39 -07:00
commit d1cd3ca5d1
3 changed files with 17 additions and 7 deletions

View File

@ -58,6 +58,8 @@ namespace cocos2d {
jobject JniHelper::classloader = NULL;
JNIEnv* JniHelper::env = NULL;
static pthread_key_t s_threadKey;
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 (&s_threadKey, 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(s_threadKey) == NULL)
pthread_setspecific(s_threadKey, _env);
return true;
}

View File

@ -60,6 +60,7 @@ public:
static jobject classloader;
private:
static void detach_current_thread (void *env);
static bool cacheEnv(JavaVM* jvm);
static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,

View File

@ -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 s_methodInitialized = false;
static void dispatch_pending_runnables() {
static cocos2d::JniMethodInfo info;
static bool initialized = false;
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));
s_methodInitialized = false;
return;
}
}