diff --git a/src/platform/Darwin/DnssdImpl.cpp b/src/platform/Darwin/DnssdImpl.cpp index a1abd6328ee156..ff0f96ffde71c5 100644 --- a/src/platform/Darwin/DnssdImpl.cpp +++ b/src/platform/Darwin/DnssdImpl.cpp @@ -89,7 +89,10 @@ void MdnsContexts::Delete(GenericContext * context) } } - DNSServiceRefDeallocate(context->serviceRef); + if (context->serviceRef != nullptr) + { + DNSServiceRefDeallocate(context->serviceRef); + } chip::Platform::Delete(context); } @@ -293,7 +296,7 @@ CHIP_ERROR Register(uint32_t interfaceId, const char * type, const char * name, sdCtx = chip::Platform::New(type, nullptr); err = DNSServiceRegister(&sdRef, 0 /* flags */, interfaceId, name, type, kLocalDot, NULL, ntohs(port), recordLen, - recordBytesPtr, OnRegister, sdCtx); + recordBytesPtr, OnRegister, sdCtx); TXTRecordDeallocate(recordRef); VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err), CHIP_ERROR_INTERNAL); @@ -485,13 +488,8 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin reinterpret_cast(&sockaddr), ttl, sdCtx); // Don't leak memory. - std::vector::iterator textEntry; - for (textEntry = sdCtx->textEntries.begin(); textEntry != sdCtx->textEntries.end(); textEntry++) - { - free(const_cast(textEntry->mKey)); - free(const_cast(textEntry->mData)); - } - + sdCtx->serviceRef = nullptr; + MdnsContexts::GetInstance().Delete(sdCtx); return CHIP_NO_ERROR; } } diff --git a/src/platform/Darwin/DnssdImpl.h b/src/platform/Darwin/DnssdImpl.h index 6e47244f2b39e2..030e7fabe3b42f 100644 --- a/src/platform/Darwin/DnssdImpl.h +++ b/src/platform/Darwin/DnssdImpl.h @@ -125,12 +125,13 @@ class MdnsContexts void SetHostname(const char * name) { mHostname = name; } const char * GetHostname() { return mHostname.c_str(); } + void Delete(GenericContext * context); + private: MdnsContexts(){}; static MdnsContexts sInstance; std::string mHostname; - void Delete(GenericContext * context); std::vector mContexts; };