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 { typedef struct command {
parser fun; parser fun;
struct command *next;
} command; } command;
void *stree_find(const syntaxtree * stree, const struct locale *lang) 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) { void stree_free(syntaxtree *stree) {
while (stree->cmds) {
command *next = stree->cmds->next;
free(stree->cmds);
stree->cmds = next;
}
while (stree) { while (stree) {
syntaxtree *snext = stree->next; syntaxtree *snext = stree->next;
freetokens(stree->root); freetokens(stree->root);
@ -66,6 +72,18 @@ syntaxtree *stree_create(void)
return sroot; 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 void
add_command(struct tnode **keys, add_command(struct tnode **keys,
const char *str, parser fun) const char *str, parser fun)

View File

@ -20,11 +20,13 @@ extern "C" {
struct order; struct order;
struct unit; struct unit;
struct tnode; struct tnode;
struct command;
typedef struct syntaxtree { typedef struct syntaxtree {
const struct locale *lang; const struct locale *lang;
struct tnode *root; struct tnode *root;
struct syntaxtree *next; struct syntaxtree *next;
struct command *cmds;
} syntaxtree; } syntaxtree;
typedef void(*parser) (const void *nodes, struct unit * u, struct order *); 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 *); void do_command(const struct tnode *troot, struct unit *u, struct order *);
struct syntaxtree *stree_create(void); struct syntaxtree *stree_create(void);
void stree_add(struct syntaxtree *, const char *str, parser fun);
void stree_free(struct syntaxtree *); void stree_free(struct syntaxtree *);
void *stree_find(const struct syntaxtree *stree, void *stree_find(const struct syntaxtree *stree,
const struct locale *lang); 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, loc, (struct locale *)st->lang);
CuAssertPtrEquals(tc, 0, st->root); CuAssertPtrEquals(tc, 0, st->root);
CuAssertPtrEquals(tc, 0, st->next); CuAssertPtrEquals(tc, 0, st->next);
add_command(&st->root, "two", parser_two); stree_add(st, "two", parser_two);
add_command(&st->root, "six", parser_six); stree_add(st, "six", parser_six);
CuAssertPtrNotNull(tc, st->root); CuAssertPtrNotNull(tc, st->root);
CuAssertPtrEquals(tc, st->root, stree_find(st, loc)); CuAssertPtrEquals(tc, st->root, stree_find(st, loc));
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));