Merge lp:~widelands-dev/widelands/bug-1734046-statistics_menu into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 8515
Proposed branch: lp:~widelands-dev/widelands/bug-1734046-statistics_menu
Merge into: lp:widelands
Diff against target: 199 lines (+20/-56)
7 files modified
src/ui_basic/unique_window.cc (+2/-21)
src/ui_basic/unique_window.h (+6/-14)
src/wui/game_options_menu.cc (+5/-2)
src/wui/game_statistics_menu.cc (+4/-6)
src/wui/game_statistics_menu.h (+0/-4)
src/wui/interactive_base.cc (+3/-5)
src/wui/interactive_base.h (+0/-4)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1734046-statistics_menu
Reviewer Review Type Date Requested Status
SirVer Approve
Review via email: mp+334398@code.launchpad.net

Commit message

Replaced UniqueWindow::Registry on_create and on_delete with boost signals. This fixes a memory problem.

Description of the change

The problem was that after the game statistics window is closed and a child windows still open, the on_delete function was still linked with boost::bind.

The windows that have graphs in them are still leaking memory, but that is a different problem. ASan report should be clear now for all other toolbar and game statistics windows.

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

two nits - always prefer lambdas over bind if you can.

review: Approve
Revision history for this message
GunChleoc (gunchleoc) wrote :

Well, in this case, it seems like I can't, except for the one in the interactive base - I'll get a heap-use-after-free because boost:signals2 will barf on it - it just doesn't like it when the window containing the button is gone, just like with the original bug.

Lambdas are nice though, they are so much easier to read! Me wants all over the code base, preciousss

@bunnybot merge

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Thanks for all the cleanup you did!

Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 2887. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/308874869.
Appveyor build 2696. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bug_1734046_statistics_menu-2696.

Revision history for this message
bunnybot (widelandsofficial) wrote :

Refusing to merge, since Travis is not green. Use @bunnybot merge force for merging anyways.

Travis build 2887. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/308874869.

Revision history for this message
GunChleoc (gunchleoc) wrote :

MacOX can't install sphinx

@bunnybot merge force

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/ui_basic/unique_window.cc'
--- src/ui_basic/unique_window.cc 2017-02-22 07:14:15 +0000
+++ src/ui_basic/unique_window.cc 2017-11-29 08:57:42 +0000
@@ -71,21 +71,6 @@
71 delete window;71 delete window;
72}72}
7373
74void UniqueWindow::Registry::assign_toggle_button(UI::Button* button) {
75 assert(!on_create);
76 assert(!on_delete);
77 on_create = boost::bind(&UI::Button::set_style, button, UI::Button::Style::kPermpressed);
78 on_delete = boost::bind(&UI::Button::set_style, button, UI::Button::Style::kRaised);
79 if (window) {
80 button->set_style(UI::Button::Style::kPermpressed);
81 }
82}
83
84void UniqueWindow::Registry::unassign_toggle_button() {
85 on_create = 0;
86 on_delete = 0;
87}
88
89/**74/**
90 * Register, position according to the registry information.75 * Register, position according to the registry information.
91*/76*/
@@ -104,9 +89,7 @@
104 set_pos(Vector2i(registry_->x, registry_->y));89 set_pos(Vector2i(registry_->x, registry_->y));
105 usedefaultpos_ = false;90 usedefaultpos_ = false;
106 }91 }
107 if (registry_->on_create) {92 registry_->opened();
108 registry_->on_create();
109 }
110 }93 }
111}94}
11295
@@ -122,9 +105,7 @@
122 registry_->y = get_y();105 registry_->y = get_y();
123 registry_->valid_pos = true;106 registry_->valid_pos = true;
124107
125 if (registry_->on_delete) {108 registry_->closed();
126 registry_->on_delete();
127 }
128 }109 }
129}110}
130}111}
131112
=== modified file 'src/ui_basic/unique_window.h'
--- src/ui_basic/unique_window.h 2017-01-25 18:55:59 +0000
+++ src/ui_basic/unique_window.h 2017-11-29 08:57:42 +0000
@@ -22,6 +22,8 @@
2222
23#include <functional>23#include <functional>
2424
25#include <boost/signals2.hpp>
26
25#include "ui_basic/button.h"27#include "ui_basic/button.h"
26#include "ui_basic/window.h"28#include "ui_basic/window.h"
2729
@@ -41,11 +43,10 @@
41 // closed.43 // closed.
42 std::function<void()> open_window;44 std::function<void()> open_window;
4345
44 // Called when the window opens.46 // Triggered when the window opens
45 std::function<void()> on_create;47 boost::signals2::signal<void()> opened;
4648 // Triggered when the window closes
47 // Called when the window is deleted (i.e. closed).49 boost::signals2::signal<void()> closed;
48 std::function<void()> on_delete;
4950
50 void create();51 void create();
51 void destroy();52 void destroy();
@@ -60,15 +61,6 @@
60 Registry() : window(nullptr), x(0), y(0), valid_pos(false) {61 Registry() : window(nullptr), x(0), y(0), valid_pos(false) {
61 }62 }
62 ~Registry();63 ~Registry();
63
64 /// The 'button' will be permpressed or not depending on whether this window is
65 /// open (on_create/on_delete callback function hooks).
66 /// This can be assigned only once.
67 void assign_toggle_button(UI::Button* button);
68
69 /// Remove the callback as a safeguard in case somewhere else in the
70 /// code someone would overwrite our hooks.
71 void unassign_toggle_button();
72 };64 };
7365
74 UniqueWindow(Panel* parent,66 UniqueWindow(Panel* parent,
7567
=== modified file 'src/wui/game_options_menu.cc'
--- src/wui/game_options_menu.cc 2017-02-21 07:56:18 +0000
+++ src/wui/game_options_menu.cc 2017-11-29 08:57:42 +0000
@@ -114,14 +114,17 @@
114 exit_game_.sigclicked.connect(114 exit_game_.sigclicked.connect(
115 boost::bind(&GameOptionsMenu::clicked_exit_game, boost::ref(*this)));115 boost::bind(&GameOptionsMenu::clicked_exit_game, boost::ref(*this)));
116116
117 windows_.sound_options.assign_toggle_button(&sound_);117 if (windows_.sound_options.window) {
118 sound_.set_style(UI::Button::Style::kPermpressed);
119 }
120 windows_.sound_options.opened.connect(boost::bind(&UI::Button::set_style, &sound_, UI::Button::Style::kPermpressed));
121 windows_.sound_options.closed.connect(boost::bind(&UI::Button::set_style, &sound_, UI::Button::Style::kRaised));
118122
119 if (get_usedefaultpos())123 if (get_usedefaultpos())
120 center_to_parent();124 center_to_parent();
121}125}
122126
123GameOptionsMenu::~GameOptionsMenu() {127GameOptionsMenu::~GameOptionsMenu() {
124 windows_.sound_options.unassign_toggle_button();
125}128}
126129
127void GameOptionsMenu::clicked_save_game() {130void GameOptionsMenu::clicked_save_game() {
128131
=== modified file 'src/wui/game_statistics_menu.cc'
--- src/wui/game_statistics_menu.cc 2017-02-21 07:56:18 +0000
+++ src/wui/game_statistics_menu.cc 2017-11-29 08:57:42 +0000
@@ -72,10 +72,11 @@
72 g_gr->images().get("images/" + image_basename + ".png"), tooltip_text);72 g_gr->images().get("images/" + image_basename + ".png"), tooltip_text);
73 box_.add(button);73 box_.add(button);
74 if (window) {74 if (window) {
75 if (!window->on_create) {75 if (window->window) {
76 window->assign_toggle_button(button);76 button->set_style(UI::Button::Style::kPermpressed);
77 registries_.push_back(*window);
78 }77 }
78 window->opened.connect(boost::bind(&UI::Button::set_style, button, UI::Button::Style::kPermpressed));
79 window->closed.connect(boost::bind(&UI::Button::set_style, button, UI::Button::Style::kRaised));
79 button->sigclicked.connect(80 button->sigclicked.connect(
80 boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(*window)));81 boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(*window)));
81 }82 }
@@ -83,7 +84,4 @@
83}84}
8485
85GameStatisticsMenu::~GameStatisticsMenu() {86GameStatisticsMenu::~GameStatisticsMenu() {
86 for (auto& registry : registries_) {
87 registry.unassign_toggle_button();
88 }
89}87}
9088
=== modified file 'src/wui/game_statistics_menu.h'
--- src/wui/game_statistics_menu.h 2017-02-12 09:10:57 +0000
+++ src/wui/game_statistics_menu.h 2017-11-29 08:57:42 +0000
@@ -47,10 +47,6 @@
47 InteractivePlayer& player_;47 InteractivePlayer& player_;
48 InteractivePlayer::GameMainMenuWindows& windows_;48 InteractivePlayer::GameMainMenuWindows& windows_;
49 UI::Box box_;49 UI::Box box_;
50
51 // These get collected by add_button
52 // so we can call unassign_toggle_button on them in the destructor.
53 std::vector<UI::UniqueWindow::Registry> registries_;
54};50};
5551
56#endif // end of include guard: WL_WUI_GAME_STATISTICS_MENU_H52#endif // end of include guard: WL_WUI_GAME_STATISTICS_MENU_H
5753
=== modified file 'src/wui/interactive_base.cc'
--- src/wui/interactive_base.cc 2017-09-17 18:17:50 +0000
+++ src/wui/interactive_base.cc 2017-11-29 08:57:42 +0000
@@ -192,9 +192,6 @@
192 if (buildroad_) {192 if (buildroad_) {
193 abort_build_road();193 abort_build_road();
194 }194 }
195 for (auto& registry : registries_) {
196 registry.unassign_toggle_button();
197 }
198}195}
199196
200const InteractiveBase::BuildhelpOverlay*197const InteractiveBase::BuildhelpOverlay*
@@ -286,8 +283,9 @@
286 g_gr->images().get("images/" + image_basename + ".png"), tooltip_text);283 g_gr->images().get("images/" + image_basename + ".png"), tooltip_text);
287 toolbar_.add(button);284 toolbar_.add(button);
288 if (window) {285 if (window) {
289 window->assign_toggle_button(button);286 window->opened.connect([this, button] { button->set_style(UI::Button::Style::kPermpressed); });
290 registries_.push_back(*window);287 window->closed.connect([this, button] { button->set_style(UI::Button::Style::kRaised); });
288
291 if (bind_default_toggle) {289 if (bind_default_toggle) {
292 button->sigclicked.connect(290 button->sigclicked.connect(
293 boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(*window)));291 boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(*window)));
294292
=== modified file 'src/wui/interactive_base.h'
--- src/wui/interactive_base.h 2017-09-13 07:27:00 +0000
+++ src/wui/interactive_base.h 2017-11-29 08:57:42 +0000
@@ -260,10 +260,6 @@
260 MapView map_view_;260 MapView map_view_;
261 ChatOverlay* chat_overlay_;261 ChatOverlay* chat_overlay_;
262262
263 // These get collected by add_toolbar_button
264 // so we can call unassign_toggle_button on them in the destructor.
265 std::vector<UI::UniqueWindow::Registry> registries_;
266
267 UI::Box toolbar_;263 UI::Box toolbar_;
268 // No unique_ptr on purpose: 'minimap_' is a UniqueWindow, its parent will264 // No unique_ptr on purpose: 'minimap_' is a UniqueWindow, its parent will
269 // delete it.265 // delete it.

Subscribers

People subscribed via source and target branches

to status/vote changes: