summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-05-05 08:57:05 +0200
committerSven Gothel <[email protected]>2022-05-05 08:57:05 +0200
commitfea8341ff36a9a3b1a27d3e45c520f3cfe3e331d (patch)
tree7001f6cc1b9f3e6b0c2aeb61d5ceb02d3d5a8fb3
parent1a6d06fff98c11e6cb762671486fbebe67382dfa (diff)
JavaGlobalObj::dtor: Hold JNIGlobalRef lock while fetching jobject and call into java mNotifyDeleted
-rw-r--r--include/jau/jni/jni_mem.hpp1
-rw-r--r--java_jni/jni/helper_jni.cxx13
2 files changed, 9 insertions, 5 deletions
diff --git a/include/jau/jni/jni_mem.hpp b/include/jau/jni/jni_mem.hpp
index ba5a282..b99f68a 100644
--- a/include/jau/jni/jni_mem.hpp
+++ b/include/jau/jni/jni_mem.hpp
@@ -80,6 +80,7 @@ extern thread_local JNIEnvContainer jni_env;
*/
class JNIGlobalRef {
private:
+ friend class JavaGlobalObj;
mutable std::mutex mtx;
jobject object;
diff --git a/java_jni/jni/helper_jni.cxx b/java_jni/jni/helper_jni.cxx
index 51f84cc..bd8a91b 100644
--- a/java_jni/jni/helper_jni.cxx
+++ b/java_jni/jni/helper_jni.cxx
@@ -341,12 +341,15 @@ jobject jau::get_new_arraylist(JNIEnv *env, jsize size, jmethodID *add)
//
JavaGlobalObj::~JavaGlobalObj() noexcept {
- jobject obj = javaObjectRef.getObject();
- if( nullptr == obj || nullptr == mNotifyDeleted ) {
- return;
- }
JNIEnv *env = *jni_env;
- env->CallVoidMethod(obj, mNotifyDeleted);
+ {
+ std::unique_lock<std::mutex> lock(javaObjectRef.mtx);
+ jobject obj = javaObjectRef.object;
+ if( nullptr == obj || nullptr == mNotifyDeleted ) {
+ return;
+ }
+ env->CallVoidMethod(obj, mNotifyDeleted);
+ }
java_exception_check_and_throw(env, E_FILE_LINE); // would abort() if thrown
}