From fadc92ee522db5501e99f2781e5508448bc905e7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 11 Jan 2016 18:17:24 +0100 Subject: [PATCH] do not leak memory for factions that have died. --- src/kernel/config.c | 7 +------ src/kernel/faction.c | 23 ++++++++++++++++++++++- src/kernel/faction.h | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 8d98165ca..bd9a30d81 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1065,12 +1065,7 @@ void free_gamedata(void) defaults[i] = 0; } } - while (factions) { - faction *f = factions; - factions = f->next; - free_faction(f); - free(f); - } + free_factions(); free_units(); free_regions(); free_borders(); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index ec80160cc..d13c1ea90 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -71,7 +71,7 @@ faction *factions; * but you should still call funhash and remove the faction from the * global list. */ -void free_faction(faction * f) +static void free_faction(faction * f) { funhash(f); if (f->alliance && f->alliance->_leader == f) { @@ -328,12 +328,33 @@ void write_faction_reference(const faction * f, struct storage *store) WRITE_INT(store, (f && f->_alive) ? f->no : 0); } +static faction *dead_factions; + +void free_flist(faction **fp) { + faction * flist = *fp; + for (flist = factions; flist;) { + faction *f = flist; + flist = f->next; + free_faction(f); + free(f); + } + *fp = 0; +} + +void free_factions(void) { + free_flist(&factions); + free_flist(&dead_factions); +} + void destroyfaction(faction ** fp) { faction * f = *fp; unit *u = f->units; *fp = f->next; + f->next = dead_factions; + dead_factions = f; + fset(f, FFL_QUIT); f->_alive = false; diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 6c76f5383..933421da5 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -136,7 +136,7 @@ extern "C" { int resolve_faction(variant data, void *addr); void renumber_faction(faction * f, int no); - void free_faction(struct faction *f); + void free_factions(void); void remove_empty_factions(void); #ifdef SMART_INTERVALS