Skip to content

Commit

Permalink
Use the existing method to clean up the context's memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
harimau-qirex committed Nov 19, 2021
1 parent da0d8ee commit 47c288e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ void MdnsContexts::Delete(GenericContext * context)
}
}

DNSServiceRefDeallocate(context->serviceRef);
if (context->serviceRef != nullptr)
{
DNSServiceRefDeallocate(context->serviceRef);
}
chip::Platform::Delete(context);
}

Expand Down Expand Up @@ -293,7 +296,7 @@ CHIP_ERROR Register(uint32_t interfaceId, const char * type, const char * name,

sdCtx = chip::Platform::New<RegisterContext>(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);
Expand Down Expand Up @@ -485,13 +488,8 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
reinterpret_cast<struct sockaddr *>(&sockaddr), ttl, sdCtx);

// Don't leak memory.
std::vector<TextEntry>::iterator textEntry;
for (textEntry = sdCtx->textEntries.begin(); textEntry != sdCtx->textEntries.end(); textEntry++)
{
free(const_cast<char *>(textEntry->mKey));
free(const_cast<uint8_t *>(textEntry->mData));
}

sdCtx->serviceRef = nullptr;
MdnsContexts::GetInstance().Delete(sdCtx);
return CHIP_NO_ERROR;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform/Darwin/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenericContext *> mContexts;
};

Expand Down

0 comments on commit 47c288e

Please sign in to comment.