From 79669e0ecd6bad5235bf6c38ac431380ef4feeca Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Sun, 3 Nov 2024 18:34:25 +0100 Subject: [PATCH] Refactor setButtonLabels() to use std::vector for safer memory management - Replaced manual dynamic memory allocation with std::vector in setButtonLabels(const button_label*...) and setButtonLabels(const std::vector&...). - Reserved vector capacity upfront to match label_count, optimizing memory usage. - Improved safety and readability by removing manual delete[] calls and handling memory cleanup through vector's automatic management. Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/646d24320f038afba1af07f7603f16055817e4be Author: Thilo Graf Date: 2024-11-03 (Sun, 03 Nov 2024) Origin message was: ------------------ Refactor setButtonLabels() to use std::vector for safer memory management - Replaced manual dynamic memory allocation with std::vector in setButtonLabels(const button_label*...) and setButtonLabels(const std::vector&...). - Reserved vector capacity upfront to match label_count, optimizing memory usage. - Improved safety and readability by removing manual delete[] calls and handling memory cleanup through vector's automatic management. ------------------ This commit was generated by Migit --- src/gui/components/cc_frm_footer.cpp | 43 +++++++++++++--------------- src/gui/components/cc_frm_footer.h | 2 +- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/gui/components/cc_frm_footer.cpp b/src/gui/components/cc_frm_footer.cpp index 40e0dfb27..7b2d211ae 100644 --- a/src/gui/components/cc_frm_footer.cpp +++ b/src/gui/components/cc_frm_footer.cpp @@ -319,39 +319,36 @@ void CComponentsFooter::setButtonLabels(const struct button_label_cc * const con void CComponentsFooter::setButtonLabels(const struct button_label * const content, const size_t& label_count, const int& chain_width, const int& label_width) { //conversion for compatibility with older paintButtons() methode, find in /gui/widget/buttons.h - button_label_cc *buttons = new button_label_cc[label_count]; - for (size_t i = 0; i< label_count; i++){ - buttons[i].button = content[i].button; - buttons[i].locale = content[i].locale; + std::vector buttons; + buttons.reserve(label_count); + + for (size_t i = 0; i < label_count; i++) { + button_label_cc button; + button.button = content[i].button; + button.locale = content[i].locale; //NOTE: here are used default values, because old button label struct don't know about this, //if it possible, don't use this methode! - buttons[i].directKeys.push_back(CRCInput::RC_nokey); - buttons[i].btn_result = -1; - buttons[i].btn_alias = -1; + button.directKeys.push_back(CRCInput::RC_nokey); + button.btn_result = -1; + button.btn_alias = -1; + buttons.emplace_back(std::move(button)); } - setButtonLabels(buttons, label_count, chain_width, label_width); - delete[] buttons; - buttons = NULL; + + setButtonLabels(buttons.data(), buttons.size(), chain_width, label_width); } -void CComponentsFooter::setButtonLabels(const vector &v_content, const int& chain_width, const int& label_width) +void CComponentsFooter::setButtonLabels(const std::vector &v_content, const int &chain_width, const int &label_width) { size_t label_count = v_content.size(); - button_label_cc *buttons = new button_label_cc[label_count]; + std::vector buttons; + buttons.reserve(label_count); - for (size_t i= 0; i< label_count; i++){ - buttons[i].button = v_content[i].button; - buttons[i].text = v_content[i].text; - buttons[i].locale = v_content[i].locale; - for (size_t j= 0; j< v_content[i].directKeys.size(); j++) - buttons[i].directKeys.push_back(v_content[i].directKeys[j]); - buttons[i].btn_result = v_content[i].btn_result; - buttons[i].btn_alias = v_content[i].btn_alias; + for (size_t i = 0; i < label_count; i++) { + button_label_cc button = v_content[i]; // Kopiere alle Felder + buttons.emplace_back(std::move(button)); } - setButtonLabels(buttons, label_count, chain_width, label_width); - delete[] buttons; - buttons = NULL; + setButtonLabels(buttons.data(), buttons.size(), chain_width, label_width); } void CComponentsFooter::setButtonLabel( const char *button_icon, diff --git a/src/gui/components/cc_frm_footer.h b/src/gui/components/cc_frm_footer.h index 6d36ef1e9..9146618d0 100644 --- a/src/gui/components/cc_frm_footer.h +++ b/src/gui/components/cc_frm_footer.h @@ -91,7 +91,7 @@ class CComponentsFooter : public CComponentsHeader, public CCButtonSelect ///add button labels with string label type as content, count as size_t, chain_width as int, label width as int void setButtonLabels(const struct button_label_cc * const content, const size_t& label_count, const int& chain_width = 0, const int& label_width = 0); ///add button labels with string label type as content, parameter 1 as vector, chain_width as int, label width as int - void setButtonLabels(const std::vector &v_content, const int& chain_width, const int& label_width); + void setButtonLabels(const std::vector &v_content, const int &chain_width, const int &label_width); ///enable/disable button frame in icon color, predefined for red, green, yellow and blue void enableButtonFrameColor(bool enable = true){btn_auto_frame_col = enable;}