fix: Fix HostObject destructors to make sure a JNI Environment is set up (#2462)

* fix: Fix HostObject destructors to make sure a JNI Environment is set up

* Use `reset` instead of `= nullptr`

* Format

* Format
This commit is contained in:
Marc Rousavy
2024-01-30 16:51:09 +01:00
committed by GitHub
parent 9e1297531e
commit 5f75b9e8dc
5 changed files with 21 additions and 9 deletions

View File

@@ -22,10 +22,9 @@ using namespace facebook;
FrameHostObject::FrameHostObject(const jni::alias_ref<JFrame::javaobject>& frame) : frame(make_global(frame)) {}
FrameHostObject::~FrameHostObject() {
// Hermes' Garbage Collector (Hades GC) calls destructors on a separate Thread
// which might not be attached to JNI. Ensure that we use the JNI class loader when
// deallocating the `frame` HybridClass, because otherwise JNI cannot call the Java
// destroy() function.
// Hermes GC might destroy HostObjects on an arbitrary Thread which might not be
// connected to the JNI environment. To make sure fbjni can properly destroy
// the Java method, we connect to a JNI environment first.
jni::ThreadScope::WithClassLoader([&] { frame.reset(); });
}