From ba110f62553fd31af02e039af2bf5bb0652d88e3 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Sun, 1 Nov 2026 14:16:03 +0100 Subject: [PATCH 1/2] fix: free JavaLocalRef handles once and call Map.put as an object method Deleting a local reference now clears the handle, a copy assignment takes the new reference before it frees the old one, and the default constructor initializes env. JavaHashMap::put uses CallObjectMethod and frees the returned previous value. --- .../jni-voithos/include/JavaRef.h | 30 +++++++++++++++---- .../jni-voithos/src/JavaHashMap.cpp | 3 +- 2 files changed, 26 insertions(+), 7 deletions(-) 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); } From 4aa4529c990ab6e8d7eeb1b62c21844f1a506eb2 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Sun, 1 Nov 2026 14:50:06 +0100 Subject: [PATCH 2/2] fix: free the session description handed to CreateSessionDescriptionObserver OnSuccess owns the description WebRTC passes in. It is now taken into a unique_ptr on entry, so it is freed after the copy to Java and on the early return when the thread cannot attach. --- .../main/cpp/src/api/CreateSessionDescriptionObserver.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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()); }