From 099b2f16ac28b422691945c206e08717938e46a3 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Thu, 12 Oct 2023 15:15:17 -0700 Subject: [PATCH] address comments --- src/controller/java/AndroidCallbacks.cpp | 8 ++++---- src/lib/support/JniTypeWrappers.h | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index 93df488d2d593f..262a54dccdb97d 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -108,7 +108,7 @@ void GetConnectedDeviceCallback::OnDeviceConnectedFn(void * context, Messaging:: JniLocalReferenceManager manager(env); // Release wrapper's global ref so application can clean up the actual callback underneath. VerifyOrReturn(self->mWrapperCallbackRef != nullptr, ChipLogError(Controller, "mWrapperCallbackRef is null")); - JniObject wrapper(env, self->mWrapperCallbackRef); + JniGlobalObjectDestructor wrapper(env, self->mWrapperCallbackRef); jclass getConnectedDeviceCallbackCls = nullptr; JniReferences::GetInstance().GetLocalClassRef( @@ -592,7 +592,7 @@ void ReportCallback::OnDone(app::ReadClient *) VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread")); JniLocalReferenceManager manager(env); VerifyOrReturn(mWrapperCallbackRef != nullptr, ChipLogError(Controller, "mWrapperCallbackRef is null")); - JniObject wrapper(env, mWrapperCallbackRef); + JniGlobalObjectDestructor wrapper(env, mWrapperCallbackRef); jmethodID onDoneMethod; err = JniReferences::GetInstance().FindMethod(env, mReportCallbackRef, "onDone", "()V", &onDoneMethod); VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onDone method")); @@ -748,7 +748,7 @@ void WriteAttributesCallback::OnDone(app::WriteClient *) VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread")); JniLocalReferenceManager manager(env); VerifyOrReturn(mWrapperCallbackRef != nullptr, ChipLogError(Controller, "mWrapperCallbackRef is null")); - JniObject wrapper(env, mWrapperCallbackRef); + JniGlobalObjectDestructor wrapper(env, mWrapperCallbackRef); jmethodID onDoneMethod; err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onDone", "()V", &onDoneMethod); VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onDone method")); @@ -868,7 +868,7 @@ void InvokeCallback::OnDone(app::CommandSender * apCommandSender) VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread")); JniLocalReferenceManager manager(env); VerifyOrReturn(mWrapperCallbackRef != nullptr, ChipLogError(Controller, "mWrapperCallbackRef is null")); - JniObject wrapper(env, mWrapperCallbackRef); + JniGlobalObjectDestructor wrapper(env, mWrapperCallbackRef); jmethodID onDoneMethod; err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onDone", "()V", &onDoneMethod); VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onDone method")); diff --git a/src/lib/support/JniTypeWrappers.h b/src/lib/support/JniTypeWrappers.h index 48d2648db268be..49a9e8c69bf2df 100644 --- a/src/lib/support/JniTypeWrappers.h +++ b/src/lib/support/JniTypeWrappers.h @@ -203,11 +203,16 @@ class JniLocalReferenceManager bool mlocalFramePushed = false; }; -class JniObject +class JniGlobalObjectDestructor { public: - JniObject(JNIEnv * aEnv, jobject aObjectRef) : mEnv(aEnv), mObjectRef(aObjectRef) {} - ~JniObject() + JniGlobalObjectDestructor(JNIEnv * aEnv, jobject & aObjectRef) : mEnv(aEnv) + { + mObjectRef = aObjectRef; + aObjectRef = nullptr; + } + + ~JniGlobalObjectDestructor() { if (mEnv != nullptr && mObjectRef != nullptr) {