From ef666a897d51441e7b04c9fbd66a7e3ea4fd3883 Mon Sep 17 00:00:00 2001 From: SendableMetatype <263203301+SendableMetatype@users.noreply.github.com> Date: Sun, 6 Sep 2026 17:52:18 +0200 Subject: [PATCH] fix: delete the local references created for received data channel messages RTCDataChannelObserver::OnMessage runs on a libwebrtc thread that stays attached to the JVM for its whole life, so JNI local references it creates are never reclaimed on their own. It created two per message and deleted neither: the direct ByteBuffer wrapping the payload, and the RTCDataChannelBuffer, which was handed to the callback with release() so the wrapper never deleted it. Every received message therefore leaked both objects until the process exited. The buffer factory now deletes the ByteBuffer reference once the RTCDataChannelBuffer holds it, and the observer passes the buffer with get() so the wrapper deletes it after the callback returns, as every other callback in the project does. --- webrtc-jni/src/main/cpp/src/api/DataBufferFactory.cpp | 3 +++ webrtc-jni/src/main/cpp/src/api/RTCDataChannelObserver.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/webrtc-jni/src/main/cpp/src/api/DataBufferFactory.cpp b/webrtc-jni/src/main/cpp/src/api/DataBufferFactory.cpp index 619b3767..2e29c4ae 100644 --- a/webrtc-jni/src/main/cpp/src/api/DataBufferFactory.cpp +++ b/webrtc-jni/src/main/cpp/src/api/DataBufferFactory.cpp @@ -31,6 +31,9 @@ namespace jni const jboolean isBinary = static_cast(dataBuffer->binary); jobject object = env->NewObject(javaClass, javaCtor, directBuffer, isBinary); + + env->DeleteLocalRef(directBuffer); + ExceptionCheck(env); return JavaLocalRef(env, object); diff --git a/webrtc-jni/src/main/cpp/src/api/RTCDataChannelObserver.cpp b/webrtc-jni/src/main/cpp/src/api/RTCDataChannelObserver.cpp index fef868e2..5c0258ea 100644 --- a/webrtc-jni/src/main/cpp/src/api/RTCDataChannelObserver.cpp +++ b/webrtc-jni/src/main/cpp/src/api/RTCDataChannelObserver.cpp @@ -43,7 +43,7 @@ namespace jni JavaLocalRef jBuffer = bufferFactory->create(env, &buffer); - env->CallVoidMethod(observer, javaClass->onMessage, jBuffer.release()); + env->CallVoidMethod(observer, javaClass->onMessage, jBuffer.get()); ExceptionCheck(env); }