Merge lp:~flegu/widelands/fix_some_memory_leaks into lp:widelands

Proposed by Jukka Pakarinen
Status: Merged
Merged at revision: 8495
Proposed branch: lp:~flegu/widelands/fix_some_memory_leaks
Merge into: lp:widelands
Diff against target: 114 lines (+14/-9)
5 files modified
src/ui_fsmenu/campaign_select.cc (+3/-3)
src/ui_fsmenu/loadgame.cc (+3/-1)
src/ui_fsmenu/options.cc (+1/-1)
src/wui/load_or_save_game.cc (+4/-3)
src/wui/load_or_save_game.h (+3/-1)
To merge this branch: bzr merge lp:~flegu/widelands/fix_some_memory_leaks
Reviewer Review Type Date Requested Status
SirVer Approve
Klaus Halfmann compile, test, review Approve
Review via email: mp+333930@code.launchpad.net

Description of the change

There happens memory leaks detected with Valgrind in multiple menu views. The changes in the branch fixes most of the cases. The leaks were found when the game was built from trunk (revno 8490) on Debian 9.1. I think these leaks are definitely happening on every operating system.

To post a comment you must log in.
Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Hellu Jukka (If got you first name correct).

Thanks for finding/fixing those Issues. We actually have
a long standing problem with Memory corruption when playing
for a longer time e.g. #1730204 so anyonw with knowledge and
tooling is welcome.

If you are a registered widelands developer,
you may consdier putting such branches into the public
space, I can assit you how to do so.

You can find me in the lobby and on widelands.org as Hasi50.
I am devloping mostly on OSX with clang / macports. But I can
switch to Win10 or Ubuntu, if needed.

Now let me check that code

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

Compiled and tested all Dialogs I could think for, ok

testing with malloc debug settings:
#! /bin/bash
export MallocGuardEdges=1
export MallocScribble=1
export MallocStackLogging=1
export MallocCheckHeapStart=0
export MallocCheckHeapEach=1
export MallocCheckHeapSleep=600

this gives an segfault on closing, but its the same on trunk.

plese check inline comment. Otherwise code LGTM.

Gun: get me mereg a private bracnh wit bunnybot and all,
or must his me moved to wideldans namespace, first.

review: Approve (compile, test, review)
Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Gun: can we merge a private branch with bunnybot and all,
or must this be moved to wideldans namespace, first?

Revision history for this message
GunChleoc (gunchleoc) wrote :

Thanks for cleaning up after me :)

Merging this with bunnybot is no problem.

I have added you to widelands-dev, so you we can work together on branches in the future. Just don't push directly to trunk :)

I think we can have a std::unique_ptr here for all the variables that you touched. It is clear who owns it, so we don't need shared_ptr, and raw pointers are something we want to get rid of.

The only raw pointers that are OK are for UI::Panel and its subclasses, because the pointers are managed well within UI::Panel and it would be a huge job to refactor that.

Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 2809. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/303894554.
Appveyor build 2620. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_flegu_widelands_fix_some_memory_leaks-2620.

Revision history for this message
Jukka Pakarinen (flegu) wrote :

> Hellu Jukka (If got you first name correct).
You got it right.

> space, I can assit you how to do so.
Thanks for offering help!

Revision history for this message
Jukka Pakarinen (flegu) wrote :

> I have added you to widelands-dev, so you we can work together on branches in
Thanks!

> the future. Just don't push directly to trunk :)
I keep that in mind!

> I think we can have a std::unique_ptr here for all the variables that you
> touched. It is clear who owns it, so we don't need shared_ptr, and raw
> pointers are something we want to get rid of.
Sounds good to me. I can try to change those to unique ones.

> The only raw pointers that are OK are for UI::Panel and its subclasses,
> because the pointers are managed well within UI::Panel and it would be a huge
> job to refactor that.
Good to know!

Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 2813. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/304031725.
Appveyor build 2624. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_flegu_widelands_fix_some_memory_leaks-2624.

Revision history for this message
GunChleoc (gunchleoc) wrote :

Code LGTM :)

You now need to add some headers to make Codecheck happy, then this can go in.

widelands/src/ui_fsmenu/loadgame.cc:117: This file uses unique_ptr but does not include <memory>.

widelands/src/wui/load_or_save_game.cc:171: This file uses unique_ptr but does not include <memory>.

widelands/src/wui/load_or_save_game.h:46: This file uses unique_ptr but does not include <memory>.

The reason for this rule is that some compilers will barf if unique_ptr is used without this header.

Documentation on how to run codecheck yourself: https://wl.widelands.org/wiki/RegressionTests/

Revision history for this message
Jukka Pakarinen (flegu) wrote :

> Documentation on how to run codecheck yourself:
> https://wl.widelands.org/wiki/RegressionTests/

Thanks for guiding me!

Revision history for this message
GunChleoc (gunchleoc) wrote :

You're welcome! We're happy to have you on board and helping with those memory leaks :)

Revision history for this message
GunChleoc (gunchleoc) wrote :

Now we're getting a compiler error:

Building CXX object src/wui/CMakeFiles/wui_common.dir/mapdetails.cc.o

/home/travis/build/widelands/widelands/src/wui/load_or_save_game.cc: In member function ‘std::unique_ptr<const SavegameData> LoadOrSaveGame::entry_selected()’:

/home/travis/build/widelands/widelands/src/wui/load_or_save_game.cc:201:9: error: could not convert ‘result’ from ‘std::unique_ptr<SavegameData>’ to ‘std::unique_ptr<const SavegameData>’

  return result;

         ^

/home/travis/build/widelands/widelands/src/wui/load_or_save_game.cc:202:1: error: control reaches end of non-void function [-Werror=return-type]

 }

 ^

Revision history for this message
Jukka Pakarinen (flegu) wrote :

That's weird! The code compiles fine with gcc 6.3.0. The code doesn't comple if I put "const" where it is missing. Maybe I just drop out the const.

Revision history for this message
SirVer (sirver) wrote :

+1e6!!!! thank you for helping fighting these leaks!

review: Approve
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 2824. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/305434917.
Appveyor build 2634. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_flegu_widelands_fix_some_memory_leaks-2634.

Revision history for this message
GunChleoc (gunchleoc) wrote :

All ready now! :D

@bunnybot merge

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/ui_fsmenu/campaign_select.cc'
--- src/ui_fsmenu/campaign_select.cc 2017-03-05 17:55:29 +0000
+++ src/ui_fsmenu/campaign_select.cc 2017-11-21 18:37:33 +0000
@@ -424,10 +424,10 @@
424 */424 */
425void FullscreenMenuCampaignMapSelect::fill_table() {425void FullscreenMenuCampaignMapSelect::fill_table() {
426 // read in the campaign config426 // read in the campaign config
427 Profile* prof;427 std::unique_ptr<Profile> prof;
428 std::string campsection;428 std::string campsection;
429 if (is_tutorial_) {429 if (is_tutorial_) {
430 prof = new Profile("campaigns/tutorials.conf", nullptr, "maps");430 prof.reset(new Profile("campaigns/tutorials.conf", nullptr, "maps"));
431431
432 // Set subtitle of the page432 // Set subtitle of the page
433 const std::string subtitle1 = _("Pick a tutorial from the list, then hit \"OK\".");433 const std::string subtitle1 = _("Pick a tutorial from the list, then hit \"OK\".");
@@ -439,7 +439,7 @@
439 campsection = "tutorials";439 campsection = "tutorials";
440440
441 } else {441 } else {
442 prof = new Profile("campaigns/campaigns.conf", nullptr, "maps");442 prof.reset(new Profile("campaigns/campaigns.conf", nullptr, "maps"));
443443
444 Section& global_s = prof->get_safe_section("global");444 Section& global_s = prof->get_safe_section("global");
445445
446446
=== modified file 'src/ui_fsmenu/loadgame.cc'
--- src/ui_fsmenu/loadgame.cc 2017-11-06 20:19:56 +0000
+++ src/ui_fsmenu/loadgame.cc 2017-11-21 18:37:33 +0000
@@ -19,6 +19,8 @@
1919
20#include "ui_fsmenu/loadgame.h"20#include "ui_fsmenu/loadgame.h"
2121
22#include <memory>
23
22#include "base/i18n.h"24#include "base/i18n.h"
23#include "wui/gamedetails.h"25#include "wui/gamedetails.h"
2426
@@ -114,7 +116,7 @@
114 return;116 return;
115 }117 }
116118
117 const SavegameData* gamedata = load_or_save_.entry_selected();119 std::unique_ptr<SavegameData> gamedata = load_or_save_.entry_selected();
118 if (gamedata && gamedata->errormessage.empty()) {120 if (gamedata && gamedata->errormessage.empty()) {
119 filename_ = gamedata->filename;121 filename_ = gamedata->filename;
120 end_modal<FullscreenMenuBase::MenuTarget>(FullscreenMenuBase::MenuTarget::kOk);122 end_modal<FullscreenMenuBase::MenuTarget>(FullscreenMenuBase::MenuTarget::kOk);
121123
=== modified file 'src/ui_fsmenu/options.cc'
--- src/ui_fsmenu/options.cc 2017-11-12 08:04:06 +0000
+++ src/ui_fsmenu/options.cc 2017-11-21 18:37:33 +0000
@@ -450,7 +450,7 @@
450450
451 std::string name = i18n::make_ligatures(table->get_string("name").c_str());451 std::string name = i18n::make_ligatures(table->get_string("name").c_str());
452 const std::string sortname = table->get_string("sort_name");452 const std::string sortname = table->get_string("sort_name");
453 LanguageEntry* entry = new LanguageEntry(localename, name);453 std::unique_ptr<LanguageEntry> entry(new LanguageEntry(localename, name));
454 entries.insert(std::make_pair(sortname, *entry));454 entries.insert(std::make_pair(sortname, *entry));
455 language_entries_.insert(std::make_pair(localename, *entry));455 language_entries_.insert(std::make_pair(localename, *entry));
456456
457457
=== modified file 'src/wui/load_or_save_game.cc'
--- src/wui/load_or_save_game.cc 2017-11-06 20:19:56 +0000
+++ src/wui/load_or_save_game.cc 2017-11-21 18:37:33 +0000
@@ -20,6 +20,7 @@
20#include "wui/load_or_save_game.h"20#include "wui/load_or_save_game.h"
2121
22#include <ctime>22#include <ctime>
23#include <memory>
2324
24#include <boost/algorithm/string.hpp>25#include <boost/algorithm/string.hpp>
25#include <boost/format.hpp>26#include <boost/format.hpp>
@@ -168,8 +169,8 @@
168 return r1.savetimestamp < r2.savetimestamp;169 return r1.savetimestamp < r2.savetimestamp;
169}170}
170171
171const SavegameData* LoadOrSaveGame::entry_selected() {172std::unique_ptr<SavegameData> LoadOrSaveGame::entry_selected() {
172 SavegameData* result = new SavegameData();173 std::unique_ptr<SavegameData> result(new SavegameData());
173 size_t selections = table_.selections().size();174 size_t selections = table_.selections().size();
174 if (selections == 1) {175 if (selections == 1) {
175 delete_->set_tooltip(176 delete_->set_tooltip(
@@ -178,7 +179,7 @@
178 _("Delete this replay") :179 _("Delete this replay") :
179 /** TRANSLATORS: Tooltip for the delete button. The user has selected 1 file */180 /** TRANSLATORS: Tooltip for the delete button. The user has selected 1 file */
180 _("Delete this game"));181 _("Delete this game"));
181 result = &games_data_[table_.get_selected()];182 result.reset(new SavegameData(games_data_[table_.get_selected()]));
182 } else if (selections > 1) {183 } else if (selections > 1) {
183 delete_->set_tooltip(184 delete_->set_tooltip(
184 filetype_ == FileType::kReplay ?185 filetype_ == FileType::kReplay ?
185186
=== modified file 'src/wui/load_or_save_game.h'
--- src/wui/load_or_save_game.h 2017-08-12 08:03:19 +0000
+++ src/wui/load_or_save_game.h 2017-11-21 18:37:33 +0000
@@ -20,6 +20,8 @@
20#ifndef WL_WUI_LOAD_OR_SAVE_GAME_H20#ifndef WL_WUI_LOAD_OR_SAVE_GAME_H
21#define WL_WUI_LOAD_OR_SAVE_GAME_H21#define WL_WUI_LOAD_OR_SAVE_GAME_H
2222
23#include <memory>
24
23#include "logic/game.h"25#include "logic/game.h"
24#include "ui_basic/box.h"26#include "ui_basic/box.h"
25#include "ui_basic/panel.h"27#include "ui_basic/panel.h"
@@ -43,7 +45,7 @@
43 bool localize_autosave);45 bool localize_autosave);
4446
45 /// Update gamedetails and tooltips and return information about the current selection47 /// Update gamedetails and tooltips and return information about the current selection
46 const SavegameData* entry_selected();48 std::unique_ptr<SavegameData> entry_selected();
4749
48 /// Whether the table has a selection50 /// Whether the table has a selection
49 bool has_selection() const;51 bool has_selection() const;

Subscribers

People subscribed via source and target branches

to status/vote changes: