refactor free_messagelist, fix leak in test_merge_split

This commit is contained in:
Enno Rehling 2015-10-14 14:08:50 +02:00
parent 1c2922aafa
commit 2abdba9e8a
6 changed files with 32 additions and 22 deletions

View file

@ -69,13 +69,17 @@ faction *factions;
void free_faction(faction * f) void free_faction(faction * f)
{ {
funhash(f); funhash(f);
if (f->msgs) if (f->msgs) {
free_messagelist(f->msgs); free_messagelist(f->msgs->begin);
free(f->msgs);
}
while (f->battles) { while (f->battles) {
struct bmsg *bm = f->battles; struct bmsg *bm = f->battles;
f->battles = bm->next; f->battles = bm->next;
if (bm->msgs) if (bm->msgs) {
free_messagelist(bm->msgs); free_messagelist(bm->msgs->begin);
free(bm->msgs);
}
free(bm); free(bm);
} }

View file

@ -283,17 +283,14 @@ void syntax_error(const struct unit *u, struct order *ord)
extern unsigned int new_hashstring(const char *s); extern unsigned int new_hashstring(const char *s);
void free_messagelist(message_list * msgs) void free_messagelist(mlist *msgs)
{ {
struct mlist **mlistptr; struct mlist **mlistptr;
if (msgs) { for (mlistptr = &msgs; *mlistptr;) {
for (mlistptr = &msgs->begin; *mlistptr;) { struct mlist *ml = *mlistptr;
struct mlist *ml = *mlistptr; *mlistptr = ml->next;
*mlistptr = ml->next; msg_release(ml->msg);
msg_release(ml->msg); free(ml);
free(ml);
}
free(msgs);
} }
} }

View file

@ -37,7 +37,7 @@ extern "C" {
struct mlist *begin, **end; struct mlist *begin, **end;
} message_list; } message_list;
void free_messagelist(message_list * msgs); void free_messagelist(struct mlist *msgs);
typedef struct msglevel { typedef struct msglevel {
/* used to set specialized msg-levels */ /* used to set specialized msg-levels */

View file

@ -39,7 +39,7 @@ void test_message(CuTest *tc) {
static void test_merge_split(CuTest *tc) { static void test_merge_split(CuTest *tc) {
message_list *mlist = 0, *append = 0; message_list *mlist = 0, *append = 0;
struct mlist **split; struct mlist **split; // TODO: why is this a double asterisk?
message_type *mtype = mt_new("custom", NULL); message_type *mtype = mt_new("custom", NULL);
test_cleanup(); test_cleanup();
@ -57,8 +57,11 @@ static void test_merge_split(CuTest *tc) {
CuAssertPtrEquals(tc, append->begin, mlist->begin->next); CuAssertPtrEquals(tc, append->begin, mlist->begin->next);
split_messages(mlist, split); split_messages(mlist, split);
CuAssertPtrEquals(tc, 0, mlist->begin->next); CuAssertPtrEquals(tc, 0, mlist->begin->next);
free_messagelist(mlist); free_messagelist(*split);
free_messagelist(append); free_messagelist(mlist->begin);
free(mlist);
free_messagelist(append->begin);
free(append);
test_cleanup(); test_cleanup();
} }

View file

@ -820,15 +820,18 @@ void free_region(region * r)
freeland(r->land); freeland(r->land);
if (r->msgs) { if (r->msgs) {
free_messagelist(r->msgs); free_messagelist(r->msgs->begin);
free(r->msgs);
r->msgs = 0; r->msgs = 0;
} }
while (r->individual_messages) { while (r->individual_messages) {
struct individual_message *msg = r->individual_messages; struct individual_message *msg = r->individual_messages;
r->individual_messages = msg->next; r->individual_messages = msg->next;
if (msg->msgs) if (msg->msgs) {
free_messagelist(msg->msgs); free_messagelist(msg->msgs->begin);
free(msg->msgs);
}
free(msg); free(msg);
} }

View file

@ -286,8 +286,11 @@ struct message * test_find_messagetype(struct message_list *msgs, const char *na
} }
void test_clear_messages(faction *f) { void test_clear_messages(faction *f) {
free_messagelist(f->msgs); if (f->msgs) {
f->msgs = 0; free_messagelist(f->msgs->begin);
free(f->msgs);
f->msgs = 0;
}
} }
void disabled_test(void *suite, void (*test)(CuTest *), const char *name) { void disabled_test(void *suite, void (*test)(CuTest *), const char *name) {