From 9fc47ad7f87e7140fb44abb181a8a2747445c6b0 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 23 Oct 2024 14:51:11 +0200 Subject: [PATCH] [darwin-framework-tool] WriteAttributeCommandBridge.h does not release the custom argument data, making the leak detector complaining --- .../clusters/WriteAttributeCommandBridge.h | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h b/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h index 66003042cccbe4..f274ea21aa654b 100644 --- a/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h +++ b/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h @@ -49,23 +49,8 @@ class WriteAttribute : public ModelCommand { CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override { - chip::TLV::TLVWriter writer; - chip::TLV::TLVReader reader; - - mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); - VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY); - - writer.Init(mData, mDataMaxLen); - - ReturnErrorOnFailure(mAttributeValue.Encode(writer, chip::TLV::AnonymousTag())); - reader.Init(mData, writer.GetLengthWritten()); - ReturnErrorOnFailure(reader.Next()); - - id value = NSObjectFromCHIPTLV(&reader); - if (value == nil) { - return CHIP_ERROR_INTERNAL; - } - + id value; + ReturnErrorOnFailure(GetValue(&value)); return WriteAttribute::SendCommand(device, endpointId, mClusterId, mAttributeId, value); } @@ -122,6 +107,35 @@ class WriteAttribute : public ModelCommand { chip::Optional mDataVersion; private: + CHIP_ERROR GetValue(id _Nonnull * _Nonnull outValue) + { + CHIP_ERROR err = CHIP_NO_ERROR; + chip::TLV::TLVWriter writer; + chip::TLV::TLVReader reader; + + mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); + VerifyOrExit(mData != nullptr, err = CHIP_ERROR_NO_MEMORY); + + writer.Init(mData, mDataMaxLen); + + err = mAttributeValue.Encode(writer, chip::TLV::AnonymousTag()); + SuccessOrExit(err); + + reader.Init(mData, writer.GetLengthWritten()); + err = reader.Next(); + SuccessOrExit(err); + + *outValue = NSObjectFromCHIPTLV(&reader); + VerifyOrDo(nil != *outValue, err = CHIP_ERROR_INTERNAL); + + exit: + if (nullptr != mData) { + chip::Platform::MemoryFree(mData); + mData = nullptr; + } + return err; + } + chip::ClusterId mClusterId; chip::AttributeId mAttributeId; CHIP_ERROR mError = CHIP_NO_ERROR;