Skip to content

Commit 6873518

Browse files
committed
Fix a leak of BattleUnitKills if the debrief is not shown
The destructor for a BattleUnit doesn't delete the BattleUnitKills if there is a valid Soldier* for the unit, on the assumption that the object will pass ownership to the SoldierDiary, but that ownership move only occurs on the DebriefingState, so if you happen to leave a battle in a way that doesn't show that it will leak (e.g. 'Abandon Game') This changes the BattleUnit destructor to unconditionally destroy any remaining BattleUnitKills objects, and the SoliderDiary::updateDiary() function to empty the BattleUnit's list when it takes ownership.
1 parent 8e31daf commit 6873518

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

‎src/Savegame/BattleUnit.cpp‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,9 @@ BattleUnit::~BattleUnit()
296296
{
297297
for (int i = 0; i < 5; ++i)
298298
if (_cache[i]) delete _cache[i];
299-
if (!getGeoscapeSoldier())
299+
for (std::vector<BattleUnitKills*>::const_iterator i = _statistics->kills.begin(); i != _statistics->kills.end(); ++i)
300300
{
301-
for (std::vector<BattleUnitKills*>::const_iterator i = _statistics->kills.begin(); i != _statistics->kills.end(); ++i)
302-
{
303-
delete *i;
304-
}
301+
delete *i;
305302
}
306303
delete _statistics;
307304
delete _currentAIState;

‎src/Savegame/SoldierDiary.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ void SoldierDiary::updateDiary(BattleUnitStatistics *unitStatistics, std::vector
166166
{
167167
if (allMissionStatistics->empty()) return;
168168
MissionStatistics* missionStatistics = allMissionStatistics->back();
169-
std::vector<BattleUnitKills*> unitKills = unitStatistics->kills;
169+
std::vector<BattleUnitKills*> &unitKills = unitStatistics->kills;
170170
for (std::vector<BattleUnitKills*>::const_iterator kill = unitKills.begin() ; kill != unitKills.end() ; ++kill)
171171
{
172172
(*kill)->makeTurnUnique();
173173
_killList.push_back(*kill);
174174
}
175+
unitKills.clear();
175176
if (missionStatistics->success)
176177
{
177178
if (unitStatistics->loneSurvivor)

0 commit comments

Comments
 (0)