From 798326ef399ce1cc6f8f65a6e26a3b0da2b16791 Mon Sep 17 00:00:00 2001 From: dan-du-car <62157949+dan-du-car@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:41:06 -0400 Subject: [PATCH] TelematicPlugin: Memory leak (#599) # PR Details ## Description Fix memory leaks issues with several variables created with malloc or new. ## Related Issue https://github.com/usdot-fhwa-OPS/V2X-Hub/issues/575 ## Motivation and Context ## How Has This Been Tested? Local integration test ## Types of changes - [x] Defect fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that cause existing functionality to change) ## Checklist: - [ ] I have added any new packages to the sonar-scanner.properties file - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [x] I have read the **CONTRIBUTING** document. [V2XHUB Contributing Guide](https://github.com/usdot-fhwa-OPS/V2X-Hub/blob/develop/Contributing.md) - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. --- .../TelematicBridgePlugin/src/TelematicBridgeMsgWorker.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgeMsgWorker.h b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgeMsgWorker.h index c31c5cc9c..d3fa956d6 100644 --- a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgeMsgWorker.h +++ b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgeMsgWorker.h @@ -147,7 +147,9 @@ namespace TelematicBridge { throw TelematicBridgeException("Failed to convert message with ID (=" + to_string(messageFrame->messageId) + ") to XML "); } - return string(xml_buffer.buffer); + auto output = string(xml_buffer.buffer); + FREEMEM(xml_buffer.buffer); + return output; } /** @@ -238,7 +240,10 @@ namespace TelematicBridge } else { - json["payload"] = StringToJson(cJSON_Print(msg->payload)); + // Render a cJSON entity to text for transfer/storage. Free the char* when finished. + auto payloadPtr = cJSON_Print(msg->payload); + json["payload"] = StringToJson(payloadPtr); + FREEMEM(payloadPtr); } }