From fb6925f528ba3024a3dbecf4eb88953ac00afc5d Mon Sep 17 00:00:00 2001 From: Rafael Siejakowski Date: Sat, 27 Apr 2024 20:36:03 +0200 Subject: [PATCH] Fix memory leak of ToolbarMenuButton structures Fix a leak whereby ToolbarMenuButton structures owned by a toolbar are not properly destroyed. --- src/ui/toolbar/toolbar.cpp | 33 ++++++++++++++------------------- src/ui/toolbar/toolbar.h | 5 +---- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/ui/toolbar/toolbar.cpp b/src/ui/toolbar/toolbar.cpp index 58b5388f26..78fbd2f87e 100644 --- a/src/ui/toolbar/toolbar.cpp +++ b/src/ui/toolbar/toolbar.cpp @@ -10,7 +10,6 @@ #include "toolbar.h" -#include #include #include #include @@ -92,12 +91,12 @@ void Toolbar::init_menu_btns() } // Now, start inserting menu buttons in the toolbar. - for (auto [key, value] : menu_btn_groups) { + for (auto const &[key, value] : menu_btn_groups) { // The map is lexicographically sorted on the basis of priorities. // Step 1: Find out the priority of this group. // Assumption: The last character of the class name stores the // value of the priority. - auto priority = key[key.size() - 1] - '0'; + auto priority = key.back() - '0'; // Add this menu button to the _menu_btns vector. insert_menu_btn(priority, value.first, value.second); @@ -133,10 +132,8 @@ void Toolbar::insert_menu_btn(const int priority, int group_size, menu_btn->set_visible(false); // Add this menu button to the _menu_btns vector. - std::stack> popover_children; - ToolbarMenuButton *mb_ptr = - new ToolbarMenuButton(priority, group_size, menu_btn, std::move(popover_children), std::move(toolbar_children)); - _menu_btns.push_back(mb_ptr); + _menu_btns.push_back( + std::make_unique(priority, group_size, menu_btn, std::move(toolbar_children))); } void Toolbar::measure_vfunc(Gtk::Orientation orientation, int for_size, int &min, int &nat, int &min_baseline, int &nat_baseline) const @@ -191,7 +188,7 @@ void Toolbar::_resize_handler(int width, int height) } // Now, move the toolbar_children of this menu button to the popover. - auto mb = _menu_btns[_active_mb_index]; + auto &mb = _menu_btns[_active_mb_index]; auto popover_box = dynamic_cast(mb->menu_btn->get_popover()->get_child()); _move_children(_toolbar, popover_box, mb->toolbar_children, mb->popover_children, mb->group_size); mb->menu_btn->set_visible(true); @@ -229,7 +226,7 @@ void Toolbar::_resize_handler(int width, int height) } } - auto mb = _menu_btns[_active_mb_index]; + auto &mb = _menu_btns[_active_mb_index]; // See if we have enough space to expand the topmost collapsed button. int req_size = min_size + _size_needed.top(); @@ -260,23 +257,21 @@ void Toolbar::_update_menu_btn_image(Gtk::Widget *child) Glib::ustring icon_name = "go-down"; if (auto btn = dynamic_cast(child)) { - // Find the icon name from the child image. + Glib::ustring icon; if (auto image = dynamic_cast(btn->get_child())) { - auto icon = image->get_icon_name(); - if (icon != "") { - icon_name = icon; - } + // Find the icon name from the child image. + icon = image->get_icon_name(); } else { // Find the icon name from the button itself. - auto icon = btn->get_icon_name(); - if (icon != "") { - icon_name = icon; - } + icon = btn->get_icon_name(); + } + if (!icon.empty()) { + icon_name = icon; } } auto menu_btn = _menu_btns[_active_mb_index]->menu_btn; - menu_btn->set_always_show_arrow(!(icon_name == "go-down")); + menu_btn->set_always_show_arrow(icon_name != "go-down"); menu_btn->set_icon_name(icon_name.c_str()); } diff --git a/src/ui/toolbar/toolbar.h b/src/ui/toolbar/toolbar.h index da487b1d21..7c0a9757ad 100644 --- a/src/ui/toolbar/toolbar.h +++ b/src/ui/toolbar/toolbar.h @@ -46,14 +46,11 @@ protected: private: struct ToolbarMenuButton { - // Constructor to initialize data members ToolbarMenuButton(int priority, int group_size, Gtk::MenuButton *menu_btn, - const std::stack> &popover_children, const std::stack> &toolbar_children) : priority(priority) , group_size(group_size) , menu_btn(menu_btn) - , popover_children(popover_children) , toolbar_children(toolbar_children) {} @@ -65,7 +62,7 @@ private: std::stack> toolbar_children; }; - std::vector _menu_btns; + std::vector> _menu_btns; std::stack _size_needed; int _active_mb_index = -1; bool _resizing = false; -- GitLab