Skip to content

Commit e6f4a47

Browse files
author
Github Executorch
committed
Summary: Use javaClassStatic() for class references stored in static variables - creates global references safe for persistence
findClassLocal() returns a local reference. Storing it in static auto exceptionClass = ... could result in potential 'invalid local reference:' as local references become invalid when the JNI frame ends Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent f24351a commit e6f4a47

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

extension/android/jni/jni_helper.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ namespace executorch::jni_helper {
1313
void throwExecutorchException(uint32_t errorCode, const std::string& details) {
1414
// Get the current JNI environment
1515
auto env = facebook::jni::Environment::current();
16+
if (!env) {
17+
return;
18+
}
1619

17-
// Find the Java ExecutorchRuntimeException class
18-
static auto exceptionClass = facebook::jni::findClassLocal(
19-
"org/pytorch/executorch/ExecutorchRuntimeException");
20+
// stable/global class ref — safe to cache
21+
static const auto exceptionClass =
22+
JExecutorchRuntimeException::javaClassStatic();
2023

2124
// Find the static factory method: makeExecutorchException(int, String)
2225
static auto makeExceptionMethod =

extension/android/jni/jni_helper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ namespace executorch::jni_helper {
2323
*/
2424
void throwExecutorchException(uint32_t errorCode, const std::string& details);
2525

26+
// Define the JavaClass wrapper
27+
struct JExecutorchRuntimeException
28+
: public facebook::jni::JavaClass<JExecutorchRuntimeException> {
29+
static constexpr auto kJavaDescriptor =
30+
"Lorg/pytorch/executorch/ExecutorchRuntimeException;";
31+
};
32+
2633
} // namespace executorch::jni_helper

0 commit comments

Comments
 (0)