Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ namespace jni
class JavaLocalRef<T, typename std::enable_if_t<std::is_convertible<T, jobject>::value>> : public JavaRef<T>
{
public:
JavaLocalRef() = default;
JavaLocalRef() :
env(nullptr)
{
}

JavaLocalRef(std::nullptr_t) :
env(nullptr)
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace jni

void JavaHashMap::put(const JavaRef<jobject> & key, const JavaRef<jobject> & value)
{
env->CallVoidMethod(jMap, mapClass->put, key.get(), value.get());
// Map.put() returns the previous value, a local reference of its own.
JavaLocalRef<jobject> previous(env, env->CallObjectMethod(jMap, mapClass->put, key.get(), value.get()));
ExceptionCheck(env);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "JavaUtils.h"
#include "JNI_WebRTC.h"

#include <memory>

namespace jni
{
CreateSessionDescriptionObserver::CreateSessionDescriptionObserver(JNIEnv * env, const JavaGlobalRef<jobject> & observer) :
Expand All @@ -31,14 +33,18 @@ 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<webrtc::SessionDescriptionInterface> owned(desc);

JNIEnv * env = AttachCurrentThread();

if (env == nullptr) {
return;
}

try {
JavaLocalRef<jobject> javaDesc = jni::RTCSessionDescription::toJava(env, desc);
JavaLocalRef<jobject> javaDesc = jni::RTCSessionDescription::toJava(env, owned.get());

env->CallVoidMethod(observer, javaClass->onSuccess, javaDesc.get());
}
Expand Down
Loading