summaryrefslogtreecommitdiff
path: root/platform/android/src/jni/generic_global_ref_deleter.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/src/jni/generic_global_ref_deleter.hpp')
-rw-r--r--platform/android/src/jni/generic_global_ref_deleter.hpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/platform/android/src/jni/generic_global_ref_deleter.hpp b/platform/android/src/jni/generic_global_ref_deleter.hpp
new file mode 100644
index 0000000000..4e53e0a0ce
--- /dev/null
+++ b/platform/android/src/jni/generic_global_ref_deleter.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <jni/jni.hpp>
+
+#include "../attach_env.hpp"
+
+namespace mbgl {
+namespace android {
+
+// A deleter that doesn't retain an JNIEnv handle but instead tries to attach the JVM. This means
+// it can be used on any thread to delete a global ref.
+struct GenericGlobalRefDeleter {
+ void operator()(jni::jobject* p) const {
+ if (p) {
+ auto env = AttachEnv();
+ env->DeleteGlobalRef(jni::Unwrap(p));
+ }
+ }
+};
+
+} // namespace android
+} // namespace mbgl