try to eliminate memory leak in command.test

This commit is contained in:
Enno Rehling 2016-09-07 16:39:54 +01:00
parent 1ad2775f51
commit 632f25d429
3 changed files with 23 additions and 2 deletions

View File

@ -30,6 +30,7 @@
typedef struct command {
parser fun;
struct command *next;
} command;
void *stree_find(const syntaxtree * stree, const struct locale *lang)
@ -43,6 +44,11 @@ void *stree_find(const syntaxtree * stree, const struct locale *lang)
}
void stree_free(syntaxtree *stree) {
while (stree->cmds) {
command *next = stree->cmds->next;
free(stree->cmds);
stree->cmds = next;
}
while (stree) {
syntaxtree *snext = stree->next;
freetokens(stree->root);
@ -66,6 +72,18 @@ syntaxtree *stree_create(void)
return sroot;
}
void stree_add(struct syntaxtree *stree, const char *str, parser fun) {
command *cmd = (command *)malloc(sizeof(command));
variant var;
assert(str);
cmd->fun = fun;
var.v = cmd;
cmd->next = stree->cmds;
stree->cmds = cmd;
addtoken(&stree->root, str, var);
}
void
add_command(struct tnode **keys,
const char *str, parser fun)

View File

@ -20,11 +20,13 @@ extern "C" {
struct order;
struct unit;
struct tnode;
struct command;
typedef struct syntaxtree {
const struct locale *lang;
struct tnode *root;
struct syntaxtree *next;
struct command *cmds;
} syntaxtree;
typedef void(*parser) (const void *nodes, struct unit * u, struct order *);
@ -33,6 +35,7 @@ extern "C" {
void do_command(const struct tnode *troot, struct unit *u, struct order *);
struct syntaxtree *stree_create(void);
void stree_add(struct syntaxtree *, const char *str, parser fun);
void stree_free(struct syntaxtree *);
void *stree_find(const struct syntaxtree *stree,
const struct locale *lang);

View File

@ -37,8 +37,8 @@ static void test_command(CuTest * tc) {
CuAssertPtrEquals(tc, loc, (struct locale *)st->lang);
CuAssertPtrEquals(tc, 0, st->root);
CuAssertPtrEquals(tc, 0, st->next);
add_command(&st->root, "two", parser_two);
add_command(&st->root, "six", parser_six);
stree_add(st, "two", parser_two);
stree_add(st, "six", parser_six);
CuAssertPtrNotNull(tc, st->root);
CuAssertPtrEquals(tc, st->root, stree_find(st, loc));
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));