diff --git a/sdk/android/native_api/jni/scoped_java_ref.h b/sdk/android/native_api/jni/scoped_java_ref.h index e37a992445..ac2c4f4c88 100644 --- a/sdk/android/native_api/jni/scoped_java_ref.h +++ b/sdk/android/native_api/jni/scoped_java_ref.h @@ -172,6 +172,7 @@ class ScopedJavaGlobalRef : public JavaRef { public: using JavaRef::obj_; + ScopedJavaGlobalRef() = default; explicit constexpr ScopedJavaGlobalRef(std::nullptr_t) {} ScopedJavaGlobalRef(JNIEnv* env, const JavaRef& other) : JavaRef(static_cast(env->NewGlobalRef(other.obj()))) {} @@ -185,6 +186,21 @@ class ScopedJavaGlobalRef : public JavaRef { AttachCurrentThreadIfNeeded()->DeleteGlobalRef(obj_); } + void operator=(const JavaRef& other) { + JNIEnv* env = AttachCurrentThreadIfNeeded(); + if (obj_ != nullptr) { + env->DeleteGlobalRef(obj_); + } + obj_ = other.is_null() ? nullptr : env->NewGlobalRef(other.obj()); + } + + void operator=(std::nullptr_t) { + if (obj_ != nullptr) { + AttachCurrentThreadIfNeeded()->DeleteGlobalRef(obj_); + } + obj_ = nullptr; + } + // Releases the reference to the caller. The caller *must* delete the // reference when it is done with it. Note that calling a Java method // is *not* a transfer of ownership and Release() should not be used.