Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Oct 12, 2023
1 parent 7d2c9ce commit 099b2f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down
11 changes: 8 additions & 3 deletions src/lib/support/JniTypeWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 099b2f1

Please sign in to comment.