From f06c57a1f4417196f241a27a7922dda9298c3ac8 Mon Sep 17 00:00:00 2001 From: Cristian Luis Duarte Date: Mon, 23 Jul 2018 15:51:54 -0300 Subject: [PATCH] Android - fix crash when returning from domains to Home when attempting to detach when not necessary --- android/app/src/main/cpp/native.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/cpp/native.cpp b/android/app/src/main/cpp/native.cpp index 42124bf154..82fd86b3dd 100644 --- a/android/app/src/main/cpp/native.cpp +++ b/android/app/src/main/cpp/native.cpp @@ -162,7 +162,19 @@ JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeOnCrea jvmArgs.version = JNI_VERSION_1_6; // choose your JNI version jvmArgs.name = NULL; // you might want to give the java thread a name jvmArgs.group = NULL; // you might want to assign the java thread to a ThreadGroup - jvm->AttachCurrentThread(reinterpret_cast(&myNewEnv), &jvmArgs); + + int attachedHere = 0; // know if detaching at the end is necessary + jint res = jvm->GetEnv((void**)&myNewEnv, JNI_VERSION_1_6); // checks if current env needs attaching or it is already attached + if (JNI_OK != res) { + qDebug() << "[JCRASH] GetEnv env not attached yet, attaching now.."; + res = jvm->AttachCurrentThread(reinterpret_cast(&myNewEnv), &jvmArgs); + if (JNI_OK != res) { + qDebug() << "[JCRASH] Failed to AttachCurrentThread, ErrorCode = " << res; + return; + } else { + attachedHere = 1; + } + } QAndroidJniObject string = QAndroidJniObject::fromString(a); jboolean jBackToScene = (jboolean) backToScene; @@ -175,7 +187,9 @@ JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeOnCrea myNewEnv->CallObjectMethod(hashmap, mapClassPut, QAndroidJniObject::fromString("url").object(), jArg.object()); } __interfaceActivity.callMethod("openAndroidActivity", "(Ljava/lang/String;ZLjava/util/HashMap;)V", string.object(), jBackToScene, hashmap); - jvm->DetachCurrentThread(); + if (attachedHere) { + jvm->DetachCurrentThread(); + } }); QObject::connect(&AndroidHelper::instance(), &AndroidHelper::hapticFeedbackRequested, [](int duration) {