From 149a722fc74c4204b84a8de2ac2f159bd8e3f004 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 10 Oct 2025 23:26:58 +0200 Subject: [PATCH] use commandTopic as a temporary object --- src/ha.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ha.h b/src/ha.h index ad94e11..4ed1409 100644 --- a/src/ha.h +++ b/src/ha.h @@ -152,9 +152,10 @@ namespace Ha { inline static unordered_map mapCommandIds; Command(Component* cmp, onMessage f) : f(f), cmp(cmp) { - snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", cmp->id); + auto commandTopic = buildTopic(); mapCommandTopics.insert({ string(commandTopic), this }); mapCommandIds.insert({ string(cmp->id), this }); + delete commandTopic; } virtual void onCommand(const char* msg) { @@ -166,15 +167,22 @@ namespace Ha { } protected: - char commandTopic[TOPIC_SIZE] = {}; onMessage f; void buildConfig(JsonDocument& jsonDoc) override { - jsonDoc["command_topic"] = (const char*)commandTopic; + auto commandTopic = buildTopic(); + jsonDoc["command_topic"] = commandTopic; + delete commandTopic; if (retain) jsonDoc["retain"] = true; } private: Component* cmp; + + char* buildTopic() { + char* commandTopic = new char[TOPIC_SIZE]; + snprintf(commandTopic, TOPIC_SIZE, BASE_TOPIC"/set", cmp->id); + return commandTopic; + } }; struct State : Config {