diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index 6f1d8ec301bc75..7836c0e7905c5d 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -979,14 +979,12 @@ - (void)readAttributePaths:(NSArray * _Nullable)attri } }; - AttributePathParams * attributePathParamsList = nullptr; - EventPathParams * eventPathParamsList = nullptr; + Platform::ScopedMemoryBuffer attributePathParamsList; + Platform::ScopedMemoryBuffer eventPathParamsList; if (attributes != nil) { size_t count = 0; - attributePathParamsList - = static_cast(Platform::MemoryCalloc([attributes count], sizeof(AttributePathParams))); - VerifyOrReturnError(attributePathParamsList != nullptr, CHIP_ERROR_NO_MEMORY); + VerifyOrReturnError(attributePathParamsList.Calloc([attributes count]), CHIP_ERROR_NO_MEMORY); for (MTRAttributeRequestPath * attribute in attributes) { [attribute convertToAttributePathParams:attributePathParamsList[count++]]; } @@ -994,15 +992,7 @@ - (void)readAttributePaths:(NSArray * _Nullable)attri if (events != nil) { size_t count = 0; - eventPathParamsList - = static_cast(Platform::MemoryCalloc([events count], sizeof(EventPathParams))); - if (eventPathParamsList == nullptr) { - if (attributePathParamsList != nullptr) { - Platform::MemoryFree(attributePathParamsList); - attributePathParamsList = nullptr; - } - return CHIP_ERROR_NO_MEMORY; - } + VerifyOrReturnError(eventPathParamsList.Calloc([events count]), CHIP_ERROR_NO_MEMORY); for (MTREventRequestPath * event in events) { [event convertToEventPathParams:eventPathParamsList[count++]]; } @@ -1013,13 +1003,16 @@ - (void)readAttributePaths:(NSArray * _Nullable)attri chip::app::ReadPrepareParams readParams(session); [params toReadPrepareParams:readParams]; - readParams.mpAttributePathParamsList = attributePathParamsList; + readParams.mpAttributePathParamsList = attributePathParamsList.Get(); readParams.mAttributePathParamsListSize = [attributePaths count]; - readParams.mpEventPathParamsList = eventPathParamsList; + readParams.mpEventPathParamsList = eventPathParamsList.Get(); readParams.mEventPathParamsListSize = [eventPaths count]; - auto onDone = [resultArray, interactionStatus, bridge, successCb, failureCb, attributePathParamsList, - eventPathParamsList](BufferedReadClientCallback * callback) { + AttributePathParams * attributePathParamsListToFree = attributePathParamsList.Get(); + EventPathParams * eventPathParamsListToFree = eventPathParamsList.Get(); + + auto onDone = [resultArray, interactionStatus, bridge, successCb, failureCb, attributePathParamsListToFree, + eventPathParamsListToFree](BufferedReadClientCallback * callback) { if (*interactionStatus != CHIP_NO_ERROR) { // Failure failureCb(bridge, *interactionStatus); @@ -1027,17 +1020,17 @@ - (void)readAttributePaths:(NSArray * _Nullable)attri // Success successCb(bridge, resultArray); } - if (attributePathParamsList != nullptr) { - Platform::MemoryFree(attributePathParamsList); + if (attributePathParamsListToFree != nullptr) { + Platform::MemoryFree(attributePathParamsListToFree); } - if (eventPathParamsList != nullptr) { - Platform::MemoryFree(eventPathParamsList); + if (eventPathParamsListToFree != nullptr) { + Platform::MemoryFree(eventPathParamsListToFree); } chip::Platform::Delete(callback); }; auto callback = chip::Platform::MakeUnique>( - attributePathParamsList, readParams.mAttributePathParamsListSize, eventPathParamsList, + attributePathParamsList.Get(), readParams.mAttributePathParamsListSize, eventPathParamsList.Get(), readParams.mEventPathParamsListSize, onAttributeSuccessCb, onEventSuccessCb, onFailureCb, onDone, nullptr); VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY); @@ -1058,6 +1051,8 @@ - (void)readAttributePaths:(NSArray * _Nullable)attri // callback->AdoptReadClient(std::move(readClient)); callback.release(); + attributePathParamsList.Release(); + eventPathParamsList.Release(); return err; }); std::move(*bridge).DispatchAction(self);