Skip to content

Commit 3b16bc1

Browse files
psiddhGithub Executorch
andauthored
Summary: Use javaClassStatic() for class references stored in static … (#14744)
…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: ### Summary [PLEASE REMOVE] See [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests) for ExecuTorch PR guidelines. [PLEASE REMOVE] If this PR closes an issue, please add a `Fixes #<issue-id>` line. [PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: <area>" label. For a list of available release notes labels, check out [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests). ### Test plan [PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable. Co-authored-by: Github Executorch <[email protected]>
1 parent 3d8b8d1 commit 3b16bc1

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)