do not leak memory for factions that have died.

This commit is contained in:
Enno Rehling 2016-01-11 18:17:24 +01:00
parent 20063e0e0e
commit fadc92ee52
3 changed files with 24 additions and 8 deletions

View File

@ -1065,12 +1065,7 @@ void free_gamedata(void)
defaults[i] = 0; defaults[i] = 0;
} }
} }
while (factions) { free_factions();
faction *f = factions;
factions = f->next;
free_faction(f);
free(f);
}
free_units(); free_units();
free_regions(); free_regions();
free_borders(); free_borders();

View File

@ -71,7 +71,7 @@ faction *factions;
* but you should still call funhash and remove the faction from the * but you should still call funhash and remove the faction from the
* global list. * global list.
*/ */
void free_faction(faction * f) static void free_faction(faction * f)
{ {
funhash(f); funhash(f);
if (f->alliance && f->alliance->_leader == 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); 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) void destroyfaction(faction ** fp)
{ {
faction * f = *fp; faction * f = *fp;
unit *u = f->units; unit *u = f->units;
*fp = f->next; *fp = f->next;
f->next = dead_factions;
dead_factions = f;
fset(f, FFL_QUIT); fset(f, FFL_QUIT);
f->_alive = false; f->_alive = false;

View File

@ -136,7 +136,7 @@ extern "C" {
int resolve_faction(variant data, void *addr); int resolve_faction(variant data, void *addr);
void renumber_faction(faction * f, int no); void renumber_faction(faction * f, int no);
void free_faction(struct faction *f); void free_factions(void);
void remove_empty_factions(void); void remove_empty_factions(void);
#ifdef SMART_INTERVALS #ifdef SMART_INTERVALS