← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-986611-cppcheck_memleak into lp:widelands

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/bug-986611-cppcheck_memleak into lp:widelands.

Commit message:
Fixed memory leaks with Buttons in:
- src/wui/actionconfirm.cc
- src/wui/game_main_menu_save_game.cc
- src/ui_fsmenu/launch_mpg.cc.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #986611 in widelands: "Issues reported by cppcheck"
  https://bugs.launchpad.net/widelands/+bug/986611

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-986611-cppcheck_memleak/+merge/300979

This branch fixes some of the memory leaks reported by cppcheck.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-986611-cppcheck_memleak into lp:widelands.
=== modified file 'src/ui_fsmenu/launch_mpg.cc'
--- src/ui_fsmenu/launch_mpg.cc	2016-04-15 09:01:09 +0000
+++ src/ui_fsmenu/launch_mpg.cc	2016-07-23 13:57:12 +0000
@@ -50,43 +50,38 @@
 	MapOrSaveSelectionWindow
 		(UI::Panel * parent, GameController * gc, uint32_t w, uint32_t h)
 	:
-	/** TRANSLATORS: Dialog box title for selecting between map or saved game for new multiplayer game */
-	Window(parent, "selection_window", 0, 0, w, h, _("Please select")),
-	ctrl_(gc)
+	  /** TRANSLATORS: Dialog box title for selecting between map or saved game for new multiplayer game */
+	  Window(parent, "selection_window", 0, 0, w, h, _("Please select")),
+	  // TODO(GunChleoc): Switch to Box layout
+	  space_(get_inner_h() / 10),
+	  butw_(get_inner_w() - 2 * space_),
+	  buth_((get_inner_h() - 2 * space_) / 5),
+	  button_map_(this, "map",
+	              space_, space_, butw_, buth_,
+	              g_gr->images().get("images/ui_basic/but0.png"),
+	              _("Map"), _("Select a map"), true, false),
+	  button_savegame_(this, "saved_game",
+	                   space_, space_ + buth_ + space_, butw_, buth_,
+	                   g_gr->images().get("images/ui_basic/but0.png"),
+	                   /** Translators: This is a button to select a savegame */
+	                   _("Saved Game"), _("Select a saved game"), true, false),
+	  button_cancel_(this, "cancel",
+	                 space_ + butw_ / 4, space_ + 3 * buth_ + 2 * space_, butw_ / 2, buth_,
+	                 g_gr->images().get("images/ui_basic/but1.png"),
+	                 _("Cancel"), _("Cancel selection"), true, false),
+	  ctrl_(gc)
 	{
 		center_to_parent();
 
-		uint32_t y     = get_inner_h() / 10;
-		uint32_t space = y;
-		uint32_t butw  = get_inner_w() - 2 * space;
-		uint32_t buth  = (get_inner_h() - 2 * space) / 5;
-		UI::Button * btn = new UI::Button
-			(this, "map",
-			 space, y, butw, buth,
-			 g_gr->images().get("images/ui_basic/but0.png"),
-			 _("Map"), _("Select a map"), true, false);
-		btn->sigclicked.connect
+		button_map_.sigclicked.connect
 			(boost::bind
 				 (&MapOrSaveSelectionWindow::pressedButton, boost::ref(*this),
 				  FullscreenMenuBase::MenuTarget::kNormalGame));
-
-		btn = new UI::Button
-			(this, "saved_game",
-			 space, y + buth + space, butw, buth,
-			 g_gr->images().get("images/ui_basic/but0.png"),
-			 /** Translators: This is a button to select a savegame */
-			 _("Saved Game"), _("Select a saved game"), true, false);
-		btn->sigclicked.connect
+		button_savegame_.sigclicked.connect
 			(boost::bind
 				 (&MapOrSaveSelectionWindow::pressedButton, boost::ref(*this),
 				  FullscreenMenuBase::MenuTarget::kScenarioGame));
-
-		btn = new UI::Button
-			(this, "cancel",
-			 space + butw / 4, y + 3 * buth + 2 * space, butw / 2, buth,
-			 g_gr->images().get("images/ui_basic/but1.png"),
-			 _("Cancel"), _("Cancel selection"), true, false);
-		btn->sigclicked.connect
+		button_cancel_.sigclicked.connect
 			(boost::bind
 				 (&MapOrSaveSelectionWindow::pressedButton, boost::ref(*this),
 				  FullscreenMenuBase::MenuTarget::kBack));
@@ -101,8 +96,13 @@
 	void pressedButton(FullscreenMenuBase::MenuTarget i) {
 		end_modal<FullscreenMenuBase::MenuTarget>(i);
 	}
-	private:
-		GameController * ctrl_;
+
+private:
+	uint32_t space_, butw_, buth_;
+	UI::Button button_map_;
+	UI::Button button_savegame_;
+	UI::Button button_cancel_;
+	GameController * ctrl_;
 };
 
 FullscreenMenuLaunchMPG::FullscreenMenuLaunchMPG

=== modified file 'src/wui/actionconfirm.cc'
--- src/wui/actionconfirm.cc	2016-03-29 08:17:14 +0000
+++ src/wui/actionconfirm.cc	2016-07-23 13:57:12 +0000
@@ -49,6 +49,10 @@
 
 protected:
 	Widelands::ObjectPointer object_;
+private:
+	UI::MultilineTextarea message_;
+	UI::Button button_ok_;
+	UI::Button button_cancel_;
 };
 
 /**
@@ -137,28 +141,22 @@
 	:
 	UI::Window
 		(&parent, "building_action_confirm", 0, 0, 200, 120, windowtitle),
-	object_ (&map_object)
+	object_ (&map_object),
+   message_(this, 0, 0, 200, 74, message, UI::Align::kCenter),
+   button_ok_(this, "ok",
+              UI::g_fh1->fontset()->is_rtl() ? 6 : 114, 80, 80, 34,
+              g_gr->images().get("images/ui_basic/but4.png"),
+              g_gr->images().get("images/wui/menu_okay.png")),
+   button_cancel_(this, "abort",
+                  UI::g_fh1->fontset()->is_rtl() ? 114 : 6, 80, 80, 34,
+                  g_gr->images().get("images/ui_basic/but4.png"),
+                  g_gr->images().get("images/wui/menu_abort.png"))
 {
-	new UI::MultilineTextarea(this, 0, 0, 200, 74, message, UI::Align::kCenter);
-
-	UI::Button * okbtn =
-		new UI::Button
-			(this, "ok",
-			 UI::g_fh1->fontset()->is_rtl() ? 6 : 114, 80, 80, 34,
-			 g_gr->images().get("images/ui_basic/but4.png"),
-			 g_gr->images().get("images/wui/menu_okay.png"));
-	okbtn->sigclicked.connect(boost::bind(&ActionConfirm::ok, this));
-
-	UI::Button * cancelbtn =
-		new UI::Button
-			(this, "abort",
-			 UI::g_fh1->fontset()->is_rtl() ? 114 : 6, 80, 80, 34,
-			 g_gr->images().get("images/ui_basic/but4.png"),
-			 g_gr->images().get("images/wui/menu_abort.png"));
-	cancelbtn->sigclicked.connect(boost::bind(&ActionConfirm::die, this));
+	button_ok_.sigclicked.connect(boost::bind(&ActionConfirm::ok, this));
+	button_cancel_.sigclicked.connect(boost::bind(&ActionConfirm::die, this));
 
 	center_to_parent();
-	cancelbtn->center_mouse();
+	button_cancel_.center_mouse();
 }
 
 

=== modified file 'src/wui/game_main_menu_save_game.cc'
--- src/wui/game_main_menu_save_game.cc	2016-04-23 13:33:53 +0000
+++ src/wui/game_main_menu_save_game.cc	2016-07-23 13:57:12 +0000
@@ -78,36 +78,28 @@
 		(this, DESCRIPTION_X, 110, 0, 20, _("Win condition:"), UI::Align::kCenterLeft),
 	win_condition_
 		(this, DESCRIPTION_X, 125, 0, 20, " ",             UI::Align::kCenterLeft),
+   button_ok_(this, "ok",
+              DESCRIPTION_X, OK_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
+              g_gr->images().get("images/ui_basic/but4.png"),
+              _("OK"),
+              std::string(),
+              false),
+   button_cancel_(this, "cancel",
+              DESCRIPTION_X, CANCEL_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
+              g_gr->images().get("images/ui_basic/but4.png"),
+              _("Cancel")),
+   button_delete_(this, "delete",
+              DESCRIPTION_X, DELETE_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
+              g_gr->images().get("images/ui_basic/but4.png"),
+              _("Delete")),
 	curdir_(SaveHandler::get_base_dir())
 {
 	editbox_.changed.connect(boost::bind(&GameMainMenuSaveGame::edit_box_changed, this));
 	editbox_.ok.connect(boost::bind(&GameMainMenuSaveGame::ok, this));
 
-	button_ok_ =
-		new UI::Button
-			(this, "ok",
-			 DESCRIPTION_X, OK_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
-			 g_gr->images().get("images/ui_basic/but4.png"),
-			 _("OK"),
-			 std::string(),
-			 false);
-	button_ok_->sigclicked.connect(boost::bind(&GameMainMenuSaveGame::ok, this));
-
-	UI::Button * cancelbtn =
-		new UI::Button
-			(this, "cancel",
-			 DESCRIPTION_X, CANCEL_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
-			 g_gr->images().get("images/ui_basic/but4.png"),
-			 _("Cancel"));
-	cancelbtn->sigclicked.connect(boost::bind(&GameMainMenuSaveGame::die, this));
-
-	UI::Button * deletebtn =
-		new UI::Button
-			(this, "delete",
-			 DESCRIPTION_X, DELETE_Y, DESCRIPTION_WIDTH, BUTTON_HEIGHT,
-			 g_gr->images().get("images/ui_basic/but4.png"),
-			 _("Delete"));
-	deletebtn->sigclicked.connect(boost::bind(&GameMainMenuSaveGame::delete_clicked, this));
+	button_ok_.sigclicked.connect(boost::bind(&GameMainMenuSaveGame::ok, this));
+	button_cancel_.sigclicked.connect(boost::bind(&GameMainMenuSaveGame::die, this));
+	button_delete_.sigclicked.connect(boost::bind(&GameMainMenuSaveGame::delete_clicked, this));
 
 	ls_.selected.connect(boost::bind(&GameMainMenuSaveGame::selected, this, _1));
 	ls_.double_clicked.connect(boost::bind(&GameMainMenuSaveGame::double_clicked, this, _1));
@@ -156,7 +148,7 @@
 	{
 		editbox_.set_text(FileSystem::filename_without_ext(name.c_str()));
 	}
-	button_ok_->set_enabled(true);
+	button_ok_.set_enabled(true);
 
 	// Try to translate the map name.
 	{
@@ -228,7 +220,7 @@
  * The editbox was changed. Enable ok button
  */
 void GameMainMenuSaveGame::edit_box_changed() {
-	button_ok_->set_enabled(editbox_.text().size());
+	button_ok_.set_enabled(editbox_.text().size());
 }
 
 static void dosave

=== modified file 'src/wui/game_main_menu_save_game.h'
--- src/wui/game_main_menu_save_game.h	2016-01-28 07:24:21 +0000
+++ src/wui/game_main_menu_save_game.h	2016-07-23 13:57:12 +0000
@@ -56,7 +56,9 @@
 
 	UI::Textarea name_label_, mapname_, gametime_label_, gametime_, players_label_,
 		win_condition_label_, win_condition_;
-	UI::Button * button_ok_;
+	UI::Button button_ok_;
+	UI::Button button_cancel_;
+	UI::Button button_delete_;
 	std::string curdir_;
 	std::string parentdir_;
 	std::string filename_;


Follow ups