diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java index 46401af8909cd2..49076867a9cc75 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java @@ -216,7 +216,6 @@ public void onResume() { public void onPause() { super.onPause(); Log.i(TAG, "onPause() called"); - stopDiscovery(); } /** Interface for notifying the host. */ diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp index df79e60bc96cf2..26526dfdac8f07 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp @@ -246,7 +246,7 @@ JNI_METHOD(jobject, removeCastingPlayerChangeListener)(JNIEnv * env, jobject, jo return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR); } - else if (DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.ObjectRef() == nullptr) + else if (!DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.HasValidObjectRef()) { ChipLogError( AppServer, diff --git a/src/platform/android/DnssdImpl.cpp b/src/platform/android/DnssdImpl.cpp index b272ed5a712414..290b3497ea11d4 100644 --- a/src/platform/android/DnssdImpl.cpp +++ b/src/platform/android/DnssdImpl.cpp @@ -191,6 +191,8 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine std::string serviceType = GetFullTypeWithSubTypes(type, protocol); JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV, + ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdBrowse")); UtfString jniServiceType(env, serviceType.c_str()); env->CallVoidMethod(sBrowserObject.ObjectRef(), sBrowseMethod, jniServiceType.jniValue(), reinterpret_cast(callback), @@ -204,7 +206,9 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine return CHIP_JNI_ERROR_EXCEPTION_THROWN; } - auto sdCtx = chip::Platform::New(callback); + auto sdCtx = chip::Platform::New(callback); + VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY, + ChipLogError(Discovery, "Failed allocate memory for BrowseContext in ChipDnssdBrowse")); *browseIdentifier = reinterpret_cast(sdCtx); return CHIP_NO_ERROR; @@ -212,14 +216,19 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) { + VerifyOrReturnError(browseIdentifier != 0, CHIP_ERROR_INVALID_ARGUMENT, + ChipLogError(Discovery, "ChipDnssdStopBrowse Invalid argument browseIdentifier = 0")); VerifyOrReturnError(sBrowserObject.HasValidObjectRef() && sStopBrowseMethod != nullptr, CHIP_ERROR_INVALID_ARGUMENT); JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - auto ctx = reinterpret_cast(browseIdentifier); + VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV, + ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdStopBrowse")); + auto ctx = reinterpret_cast(browseIdentifier); env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, reinterpret_cast(ctx->callback)); chip::Platform::Delete(ctx); + ctx = nullptr; if (env->ExceptionCheck()) { ChipLogError(Discovery, "Java exception in ChipDnssdStopBrowse"); @@ -339,6 +348,12 @@ void InitializeWithObjects(jobject resolverObject, jobject browserObject, jobjec env->ExceptionClear(); } + if (sStopBrowseMethod == nullptr) + { + ChipLogError(Discovery, "Failed to access Discover 'stopDiscover' method"); + env->ExceptionClear(); + } + if (sGetTextEntryKeysMethod == nullptr) { ChipLogError(Discovery, "Failed to access MdnsCallback 'getTextEntryKeys' method");