From 77594581ffba9fd217ea8306bea7cad79299ada0 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 16 May 2024 10:26:15 +0200 Subject: [PATCH] return string when building protocol ids - avoid unused pointers --- gateway/include/Protocol_1.h | 7 +++---- gateway/include/Protocol_2.h | 12 ------------ gateway/include/devices.h | 8 +++++++- gateway/include/ha.h | 4 ++-- gateway/include/huzzah.h | 3 +-- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index ccbc329..6d9dd85 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -27,10 +27,9 @@ public: rcSwitch["raw_value"] = value; } - static char* buildId(const char* group, const unsigned char channel) { - char* uId = new char[30]; + static std::string buildId(const char* group, const unsigned char channel) { + char uId[30]; sprintf(uId, "%s_%d", group, channel); - return uId; + return std::string{ uId }; } - }; diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index f618898..589154e 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -33,16 +33,4 @@ public: } } - static char* buildId(const char* id) { - char* uId = new char[30]; - sprintf(uId, "%s", id); - return uId; - } - - static char* buildId(unsigned int id) { - char* uId = new char[30]; - sprintf(uId, "%d", id); - return uId; - } - }; \ No newline at end of file diff --git a/gateway/include/devices.h b/gateway/include/devices.h index c3b8599..0809e01 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -55,7 +55,13 @@ struct PollinSwitch : Switch { unsigned char channel; PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr) - : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) { + : Switch(name, [group, channel]{ + // copy id from string into a new pointer, to avoid memory leaks + string s = Protocol_1::buildId(group, channel); + char* uId = new char[s.length() + 1]; + strcpy(uId, s.c_str()); + return uId; + }()), group(group), channel(channel) { mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice); deviceClass = "outlet"; p1Switches.insert({ string(id), this }); diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 4ec2a45..ad354ff 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -233,11 +233,11 @@ namespace Ha { const char* unitMeasure = nullptr; const char* valueTemplate = nullptr; - Sensor() : Component(name, Protocol_2::buildId(id), "sensor") { + Sensor() : Component(name, id, "sensor") { withStateTopic(); } - Sensor(const char* name, const char* id) : Component(name, Protocol_2::buildId(id), "sensor") { + Sensor(const char* name, const char* id) : Component(name, id, "sensor") { withStateTopic(); } diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index b286363..42fd882 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -56,7 +56,6 @@ namespace Board { JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; switch ((unsigned int)rcSwitch["protocol"]) { case 1: { - // buildId returns a new pointer, should it be deleted, or string will take care of it? string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]); Ha::Switch* el = p1Switches[id]; if (el != nullptr) { @@ -81,7 +80,7 @@ namespace Board { } if (jsonDoc.containsKey("sensor")) { JsonObjectConst json = jsonDoc["sensor"]; - string id = Protocol_2::buildId((unsigned int)json["id"]); + string id = to_string((unsigned int)json["id"]); char stateTopic[TOPIC_SIZE]; sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str()); Mqtt::publish(stateTopic, message);