[energy-scan-server] use OwnedPtr to prevent memory leak (#11678)

This commit updates the `EnergyScanServer` to use an `OwnedPtr<>` for
its `mReportMessage` member.

This change ensures that the allocated report message is correctly
freed when the `otInstance` is destroyed, preventing a potential
memory leak.
This commit is contained in:
Abtin Keshavarzian
2025-07-08 15:13:07 -07:00
committed by GitHub
parent a2fb36cd0b
commit 85f0e8e0ac
2 changed files with 15 additions and 18 deletions
+5 -9
View File
@@ -46,7 +46,6 @@ EnergyScanServer::EnergyScanServer(Instance &aInstance)
, mPeriod(0)
, mScanDuration(0)
, mCount(0)
, mReportMessage(nullptr)
, mTimer(aInstance)
{
}
@@ -68,8 +67,7 @@ void EnergyScanServer::HandleTmf<kUriEnergyScan>(Coap::Message &aMessage, const
SuccessOrExit(MeshCoP::ChannelMaskTlv::FindIn(aMessage, mask));
FreeMessage(mReportMessage);
mReportMessage = Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(kUriEnergyReport);
mReportMessage.Reset(Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(kUriEnergyReport));
VerifyOrExit(mReportMessage != nullptr);
SuccessOrExit(MeshCoP::ChannelMaskTlv::AppendTo(*mReportMessage, mask));
@@ -130,8 +128,7 @@ void EnergyScanServer::HandleScanResult(Mac::EnergyScanResult *aResult)
{
if (mReportMessage->Append<int8_t>(aResult->mMaxRssi) != kErrorNone)
{
FreeMessage(mReportMessage);
mReportMessage = nullptr;
mReportMessage.Free();
ExitNow();
}
@@ -184,13 +181,13 @@ void EnergyScanServer::SendReport(void)
messageInfo.SetSockAddrToRlocPeerAddrTo(mCommissioner);
SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*mReportMessage, messageInfo));
mReportMessage.Release();
LogInfo("Sent %s", UriToString<kUriEnergyReport>());
exit:
FreeMessageOnError(mReportMessage, error);
LogWarnOnError(error, "send scan results");
mReportMessage = nullptr;
mReportMessage.Free();
}
void EnergyScanServer::HandleNotifierEvents(Events aEvents)
@@ -200,8 +197,7 @@ void EnergyScanServer::HandleNotifierEvents(Events aEvents)
if (aEvents.Contains(kEventThreadNetdataChanged) && (mReportMessage != nullptr) &&
Get<NetworkData::Leader>().FindBorderAgentRloc(borderAgentRloc) != kErrorNone)
{
mReportMessage->Free();
mReportMessage = nullptr;
mReportMessage.Free();
mTimer.Stop();
}
}
+10 -9
View File
@@ -39,6 +39,7 @@
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "common/notifier.hpp"
#include "common/owned_ptr.hpp"
#include "common/timer.hpp"
#include "mac/mac.hpp"
#include "net/ip6_address.hpp"
@@ -79,15 +80,15 @@ private:
using ScanTimer = TimerMilliIn<EnergyScanServer, &EnergyScanServer::HandleTimer>;
Ip6::Address mCommissioner;
uint32_t mChannelMask;
uint32_t mChannelMaskCurrent;
uint16_t mPeriod;
uint16_t mScanDuration;
uint8_t mCount;
uint8_t mNumScanResults;
Coap::Message *mReportMessage;
ScanTimer mTimer;
Ip6::Address mCommissioner;
uint32_t mChannelMask;
uint32_t mChannelMaskCurrent;
uint16_t mPeriod;
uint16_t mScanDuration;
uint8_t mCount;
uint8_t mNumScanResults;
OwnedPtr<Coap::Message> mReportMessage;
ScanTimer mTimer;
};
DeclareTmfHandler(EnergyScanServer, kUriEnergyScan);