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
1=== modified file 'src/ui_fsmenu/campaign_select.cc'
2--- src/ui_fsmenu/campaign_select.cc 2017-03-05 17:55:29 +0000
3+++ src/ui_fsmenu/campaign_select.cc 2017-11-21 18:37:33 +0000
4@@ -424,10 +424,10 @@
5 */
6 void FullscreenMenuCampaignMapSelect::fill_table() {
7 // read in the campaign config
8- Profile* prof;
9+ std::unique_ptr<Profile> prof;
10 std::string campsection;
11 if (is_tutorial_) {
12- prof = new Profile("campaigns/tutorials.conf", nullptr, "maps");
13+ prof.reset(new Profile("campaigns/tutorials.conf", nullptr, "maps"));
14
15 // Set subtitle of the page
16 const std::string subtitle1 = _("Pick a tutorial from the list, then hit \"OK\".");
17@@ -439,7 +439,7 @@
18 campsection = "tutorials";
19
20 } else {
21- prof = new Profile("campaigns/campaigns.conf", nullptr, "maps");
22+ prof.reset(new Profile("campaigns/campaigns.conf", nullptr, "maps"));
23
24 Section& global_s = prof->get_safe_section("global");
25
26
27=== modified file 'src/ui_fsmenu/loadgame.cc'
28--- src/ui_fsmenu/loadgame.cc 2017-11-06 20:19:56 +0000
29+++ src/ui_fsmenu/loadgame.cc 2017-11-21 18:37:33 +0000
30@@ -19,6 +19,8 @@
31
32 #include "ui_fsmenu/loadgame.h"
33
34+#include <memory>
35+
36 #include "base/i18n.h"
37 #include "wui/gamedetails.h"
38
39@@ -114,7 +116,7 @@
40 return;
41 }
42
43- const SavegameData* gamedata = load_or_save_.entry_selected();
44+ std::unique_ptr<SavegameData> gamedata = load_or_save_.entry_selected();
45 if (gamedata && gamedata->errormessage.empty()) {
46 filename_ = gamedata->filename;
47 end_modal<FullscreenMenuBase::MenuTarget>(FullscreenMenuBase::MenuTarget::kOk);
48
49=== modified file 'src/ui_fsmenu/options.cc'
50--- src/ui_fsmenu/options.cc 2017-11-12 08:04:06 +0000
51+++ src/ui_fsmenu/options.cc 2017-11-21 18:37:33 +0000
52@@ -450,7 +450,7 @@
53
54 std::string name = i18n::make_ligatures(table->get_string("name").c_str());
55 const std::string sortname = table->get_string("sort_name");
56- LanguageEntry* entry = new LanguageEntry(localename, name);
57+ std::unique_ptr<LanguageEntry> entry(new LanguageEntry(localename, name));
58 entries.insert(std::make_pair(sortname, *entry));
59 language_entries_.insert(std::make_pair(localename, *entry));
60
61
62=== modified file 'src/wui/load_or_save_game.cc'
63--- src/wui/load_or_save_game.cc 2017-11-06 20:19:56 +0000
64+++ src/wui/load_or_save_game.cc 2017-11-21 18:37:33 +0000
65@@ -20,6 +20,7 @@
66 #include "wui/load_or_save_game.h"
67
68 #include <ctime>
69+#include <memory>
70
71 #include <boost/algorithm/string.hpp>
72 #include <boost/format.hpp>
73@@ -168,8 +169,8 @@
74 return r1.savetimestamp < r2.savetimestamp;
75 }
76
77-const SavegameData* LoadOrSaveGame::entry_selected() {
78- SavegameData* result = new SavegameData();
79+std::unique_ptr<SavegameData> LoadOrSaveGame::entry_selected() {
80+ std::unique_ptr<SavegameData> result(new SavegameData());
81 size_t selections = table_.selections().size();
82 if (selections == 1) {
83 delete_->set_tooltip(
84@@ -178,7 +179,7 @@
85 _("Delete this replay") :
86 /** TRANSLATORS: Tooltip for the delete button. The user has selected 1 file */
87 _("Delete this game"));
88- result = &games_data_[table_.get_selected()];
89+ result.reset(new SavegameData(games_data_[table_.get_selected()]));
90 } else if (selections > 1) {
91 delete_->set_tooltip(
92 filetype_ == FileType::kReplay ?
93
94=== modified file 'src/wui/load_or_save_game.h'
95--- src/wui/load_or_save_game.h 2017-08-12 08:03:19 +0000
96+++ src/wui/load_or_save_game.h 2017-11-21 18:37:33 +0000
97@@ -20,6 +20,8 @@
98 #ifndef WL_WUI_LOAD_OR_SAVE_GAME_H
99 #define WL_WUI_LOAD_OR_SAVE_GAME_H
100
101+#include <memory>
102+
103 #include "logic/game.h"
104 #include "ui_basic/box.h"
105 #include "ui_basic/panel.h"
106@@ -43,7 +45,7 @@
107 bool localize_autosave);
108
109 /// Update gamedetails and tooltips and return information about the current selection
110- const SavegameData* entry_selected();
111+ std::unique_ptr<SavegameData> entry_selected();
112
113 /// Whether the table has a selection
114 bool has_selection() const;

Subscribers

People subscribed via source and target branches

to status/vote changes: