diff --git a/webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaRef.h b/webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaRef.h index 44aa7833f..0670f27bb 100644 --- a/webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaRef.h +++ b/webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaRef.h @@ -87,7 +87,10 @@ namespace jni class JavaLocalRef::value>> : public JavaRef { public: - JavaLocalRef() = default; + JavaLocalRef() : + env(nullptr) + { + } JavaLocalRef(std::nullptr_t) : env(nullptr) @@ -177,22 +180,37 @@ namespace jni void retain(T obj) { - deleteLocalRef(); + // Takes the new reference before it lets go of the old one: on + // self-assignment, or with another wrapper of the same handle, + // obj is the reference deleteLocalRef() would free. + jobject ref = nullptr; if (obj != nullptr) { - this->obj = env->NewLocalRef(obj); + if (env == nullptr) { + env = AttachCurrentThread(); + } + + ref = env->NewLocalRef(obj); } + + deleteLocalRef(); + + this->obj = ref; } void deleteLocalRef() { + if (this->obj == nullptr) { + return; + } + if (env == nullptr) { env = AttachCurrentThread(); } - if (this->obj != nullptr) { - env->DeleteLocalRef(this->obj); - } + env->DeleteLocalRef(this->obj); + + this->obj = nullptr; } private: diff --git a/webrtc-jni/src/main/cpp/dependencies/jni-voithos/src/JavaHashMap.cpp b/webrtc-jni/src/main/cpp/dependencies/jni-voithos/src/JavaHashMap.cpp index 122dfeba4..60c6decb6 100644 --- a/webrtc-jni/src/main/cpp/dependencies/jni-voithos/src/JavaHashMap.cpp +++ b/webrtc-jni/src/main/cpp/dependencies/jni-voithos/src/JavaHashMap.cpp @@ -33,7 +33,8 @@ namespace jni void JavaHashMap::put(const JavaRef & key, const JavaRef & value) { - env->CallVoidMethod(jMap, mapClass->put, key.get(), value.get()); + // Map.put() returns the previous value, a local reference of its own. + JavaLocalRef previous(env, env->CallObjectMethod(jMap, mapClass->put, key.get(), value.get())); ExceptionCheck(env); } diff --git a/webrtc-jni/src/main/cpp/src/api/CreateSessionDescriptionObserver.cpp b/webrtc-jni/src/main/cpp/src/api/CreateSessionDescriptionObserver.cpp index 61478d3ca..de1d9336f 100644 --- a/webrtc-jni/src/main/cpp/src/api/CreateSessionDescriptionObserver.cpp +++ b/webrtc-jni/src/main/cpp/src/api/CreateSessionDescriptionObserver.cpp @@ -21,6 +21,8 @@ #include "JavaUtils.h" #include "JNI_WebRTC.h" +#include + namespace jni { CreateSessionDescriptionObserver::CreateSessionDescriptionObserver(JNIEnv * env, const JavaGlobalRef & observer) : @@ -31,6 +33,10 @@ namespace jni void CreateSessionDescriptionObserver::OnSuccess(webrtc::SessionDescriptionInterface * desc) { + // The callback owns desc. Java gets a copy, so it is freed on every + // path out of here, including the early return. + std::unique_ptr owned(desc); + JNIEnv * env = AttachCurrentThread(); if (env == nullptr) { @@ -38,7 +44,7 @@ namespace jni } try { - JavaLocalRef javaDesc = jni::RTCSessionDescription::toJava(env, desc); + JavaLocalRef javaDesc = jni::RTCSessionDescription::toJava(env, owned.get()); env->CallVoidMethod(observer, javaClass->onSuccess, javaDesc.get()); }