From 8090f5196c52e38e01aa1c3422e143a9e4edd84a Mon Sep 17 00:00:00 2001 From: forntoh Date: Sun, 27 Oct 2024 11:02:39 +0100 Subject: [PATCH 1/2] Refactor value handling in ItemInput class - Removed unnecessary dynamic memory allocation - Updated value assignment logic for better efficiency --- src/ItemInput.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/ItemInput.h b/src/ItemInput.h index b607e77b..99efa0ba 100644 --- a/src/ItemInput.h +++ b/src/ItemInput.h @@ -266,19 +266,17 @@ class ItemInput : public MenuItem { */ void typeChar(MenuRenderer* renderer, const unsigned char character) { uint8_t length = strlen(value); + char buf[length + 2]; if (cursor < length) { char start[length]; char end[length]; - char* joined = new char[length + 2]; substring(value, 0, cursor, start); substring(value, cursor, length - cursor, end); - concat(start, character, end, joined); - value = joined; + concat(start, character, end, buf); } else { - char* buf = new char[length + 2]; concat(value, character, buf); - value = buf; } + value = buf; cursor++; uint8_t viewSize = getViewSize(renderer); if (cursor > (view + viewSize - 1)) { @@ -293,7 +291,7 @@ class ItemInput : public MenuItem { * @brief Clear the value of the input field */ void clear(MenuRenderer* renderer) { - value = (char*)""; + value[0] = '\0'; draw(renderer); renderer->drawBlinker(); // Log From 48bb8da94cce59dbed3f1b9dee4d0c675b8d8066 Mon Sep 17 00:00:00 2001 From: forntoh Date: Sun, 27 Oct 2024 11:21:37 +0100 Subject: [PATCH 2/2] Refactor memory management in ItemInput class - Dynamically allocate buffer for value - Update value pointer after concatenation - Free previous value memory allocation --- src/ItemInput.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ItemInput.h b/src/ItemInput.h index 99efa0ba..5c30f172 100644 --- a/src/ItemInput.h +++ b/src/ItemInput.h @@ -266,7 +266,7 @@ class ItemInput : public MenuItem { */ void typeChar(MenuRenderer* renderer, const unsigned char character) { uint8_t length = strlen(value); - char buf[length + 2]; + char* buf = new char[length + 2]; if (cursor < length) { char start[length]; char end[length]; @@ -276,6 +276,7 @@ class ItemInput : public MenuItem { } else { concat(value, character, buf); } + delete[] value; value = buf; cursor++; uint8_t viewSize = getViewSize(renderer);