-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init the classloader on the Java thread as a speculative fix for Skil…
…arBabcock's crash
- Loading branch information
Showing
6 changed files
with
97 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "JniClassNames.h" | ||
|
||
#include "LogUtils.h" | ||
|
||
#include <cassert> | ||
|
||
namespace VR { | ||
namespace JniGlobalRef { | ||
jmethodID gFindClassMethodID = nullptr; | ||
jobject gClassLoader = nullptr; | ||
} // namespace JniGlobalRef | ||
} // namespace VR | ||
|
||
void VR::JNI::InitJNI(JNIEnv* jni, jobject activityObject) { | ||
assert(jni != nullptr); | ||
const jclass activityClass = jni->GetObjectClass(activityObject); | ||
if (activityClass == nullptr) { FAIL("Failed to get activity class"); } | ||
|
||
// Get the getClassLoader method ID | ||
const jmethodID getClassLoaderMethod = | ||
jni->GetMethodID(activityClass, "getClassLoader", "()Ljava/lang/ClassLoader;"); | ||
if (getClassLoaderMethod == nullptr) { FAIL("Failed to get getClassLoader method ID"); } | ||
|
||
// Call getClassLoader of the activity object to obtain the class loader | ||
const jobject classLoaderObject = jni->CallObjectMethod(activityObject, getClassLoaderMethod); | ||
if (classLoaderObject == nullptr) { FAIL("Failed to get class loader object"); } | ||
|
||
JniGlobalRef::gClassLoader = jni->NewGlobalRef(classLoaderObject); | ||
|
||
// Step 3: Cache the findClass method ID | ||
jclass classLoaderClass = jni->FindClass("java/lang/ClassLoader"); | ||
if (classLoaderClass == nullptr) { FAIL("Failed to find class loader class"); } | ||
JniGlobalRef::gFindClassMethodID = | ||
jni->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); | ||
if (JniGlobalRef::gFindClassMethodID == nullptr) { FAIL("Failed to get findClass method ID"); } | ||
|
||
// Cleanup local references | ||
jni->DeleteLocalRef(activityClass); | ||
jni->DeleteLocalRef(classLoaderClass); | ||
} | ||
|
||
void VR::JNI::CleanupJNI(JNIEnv* jni) { | ||
assert(jni != nullptr); | ||
if (JniGlobalRef::gClassLoader != nullptr) { jni->DeleteGlobalRef(JniGlobalRef::gClassLoader); } | ||
JniGlobalRef::gClassLoader = nullptr; | ||
JniGlobalRef::gFindClassMethodID = nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <jni.h> | ||
|
||
namespace VR { | ||
namespace JniGlobalRef { | ||
extern jmethodID gFindClassMethodID; | ||
extern jobject gClassLoader; | ||
|
||
} // namespace JniGlobalRef | ||
|
||
namespace JNI { | ||
// Called during JNI_OnLoad | ||
void InitJNI(JNIEnv* env, jobject activityObject); | ||
// Called during JNI_OnUnload | ||
void CleanupJNI(JNIEnv* env); | ||
} // namespace JNI | ||
} // namespace VR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters