diff --git a/src/ktkProgram.c b/src/ktkProgram.c index 4221f7e..2dfa096 100644 --- a/src/ktkProgram.c +++ b/src/ktkProgram.c @@ -14,7 +14,7 @@ int ktk_freeProgram(struct ktkProgram *program) { int i; if (program->structure_count < 1) return 1; for (i = 0; i < program->structure_count; i++) { - //ktk_deleteStructure(program->structures[i]); + ktk_deleteStructure(&(program->structures[i])); free(program->structure_name[i]); } free(program->structures); @@ -211,6 +211,7 @@ struct ktkLive *ktk_buildStructure(struct ktkProgram *program, struct ktkLive *l }*/ } } + ktk_deleteMap(&structure_map); ktk_addLiveChild(live_parent, live); break; } else if (structure->flags & ktk_STRUCTURE_OVERLAP) { @@ -465,6 +466,7 @@ int ktk_linkStructures(struct ktkProgram *program, struct ktkLive *live, struct parent_live->name = malloc(strlen(live->name)+1); memcpy(parent_live->name, live->name, strlen(live->name)+1); } + ktk_addLiveChild(new_live, parent_live); } // *** step int i; diff --git a/src/ktkStructure.c b/src/ktkStructure.c index 8287631..dd9e931 100644 --- a/src/ktkStructure.c +++ b/src/ktkStructure.c @@ -103,6 +103,46 @@ void ktk_dumpStructure(struct ktkStructure *structure) { printf("size_y: %dx%d\n", structure->size_y.a, structure->size_y.b); } +int ktk_deleteStructure(struct ktkStructure *structure) { + if (structure->name != NULL) { + free(structure->name); + structure->name = NULL; + } + ktk_deleteNumberSet(&structure->id_1); + ktk_deleteNumberSet(&structure->id_2); + ktk_deleteNumberSet(&structure->replace_id_1); + ktk_deleteNumberSet(&structure->replace_id_2); + if (structure->relation_count > 0) { + int i; + for (i = 0; i < structure->relation_count; i++) { + //ktk_deleteLive(structure->relations[i]); + //free(structure->relations[i]); + free(structure->relations[i].name); + } + free(structure->relations); + structure->relations = NULL; + structure->relation_count = 0; + } + if (structure->path_count > 0) { + int i; + for (i = 0; i < structure->path_count; i++) { + free(structure->paths[i].name); + free(structure->paths[i].to); + free(structure->paths[i].from); + ktk_deleteNumberSet(&(structure->paths[i].to_x)); + ktk_deleteNumberSet(&(structure->paths[i].to_y)); + ktk_deleteNumberSet(&(structure->paths[i].from_x)); + ktk_deleteNumberSet(&(structure->paths[i].from_y)); + ktk_deleteNumberSet(&(structure->paths[i].x)); + ktk_deleteNumberSet(&(structure->paths[i].y)); + } + free(structure->paths); + structure->paths = NULL; + structure->path_count = 0; + } + return 0; +} + const struct ktkRelation ktk_RELATION_DEFAULT = { 0, NULL, @@ -197,7 +237,7 @@ struct ktkLive *ktk_getLive(struct ktkLive *live, const char *get, int match_off int ktk_addLink(struct ktkLive *live_1, struct ktkLive *live_2) { live_1->link_count++; - live_1->links = realloc(live_1->links, sizeof(live_1->link_count*sizeof(struct ktkLive*))); + live_1->links = realloc(live_1->links, live_1->link_count*sizeof(struct ktkLive*)); live_1->links[live_1->link_count-1] = live_2; return 0; } @@ -226,6 +266,10 @@ int ktk_deleteLive(struct ktkLive *live) { live->children = NULL; live->child_count = 0; } + if (live->link_count > 0) { + free(live->links); + live->links = NULL; + } return 0; } @@ -262,7 +306,7 @@ const struct ktkPath ktk_PATH_DEFAULT = { }; int ktk_explodeString(const char *string, char delim, int depth, char ***results) { - int words = 0, word_size = 0, word_i = 0; + int words = 0, word_size = 1, word_i = 0; int i = 0; int len = strlen(string)+1; // TODO: resize in 8-byte chunks or so @@ -274,7 +318,8 @@ int ktk_explodeString(const char *string, char delim, int depth, char ***results *results = realloc(*results, (++words+1)*sizeof(char*)); (*results)[words] = malloc(1*sizeof(char)); //(*results)[words][0] = '\0'; - word_i = word_size = 0; + word_i = 0; + word_size = 1; } else { (*results)[words] = realloc((*results)[words], ++word_size*sizeof(char)); (*results)[words][word_i++] = string[i]; diff --git a/src/ktk_parse.c b/src/ktk_parse.c index 4364eca..ccb4140 100644 --- a/src/ktk_parse.c +++ b/src/ktk_parse.c @@ -24,7 +24,7 @@ struct ktkNumber ktk_parseNumber(const char *text) { number.type |= ktk_ISSET; } // free words - for (w = 0; w < word_count; w++) { + for (w = 0; w <= word_count; w++) { free(words[w]); } free(words); @@ -43,6 +43,7 @@ struct ktkNumberSet *ktk_parseNumberSet(struct ktkNumberSet *set, const char *te set->sets[w] = ktk_parseNumber(words[w]); free(words[w]); } + free(words[w]); free(words); return set; @@ -135,7 +136,8 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { // ** end structure reading logic } else if (state == ktk_PARSE_RELATIONS) { // ** relation reading logic - if ((word_count = ktk_explodeString(current_read, ' ', 1, &words)) >= 1) { + word_count = ktk_explodeString(current_read, ' ', 1, &words); + if (word_count > 1) { if (words[1][0] == '{') { // I'm bad. :) program->structures[program->structure_count-1].relation_count++; @@ -145,7 +147,9 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { program->structures[program->structure_count-1].relations[program->structures[program->structure_count-1].relation_count-1].name = malloc(name_size*sizeof(char)); memcpy(program->structures[program->structure_count-1].relations[program->structures[program->structure_count-1].relation_count-1].name, words[0], name_size); state = ktk_PARSE_RELATION_STRUCTURE; - } else if (words[0][0] == '}') { + } + } else { + if (words[0][0] == '}') { state = ktk_PARSE_STRUCTURE; } } @@ -178,7 +182,7 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { // ** paths reading logic } else if (state == ktk_PARSE_PATHS) { word_count = ktk_explodeString(current_read, ' ', 1, &words); - if (word_count >= 1) { + if (word_count > 1) { if (words[1][0] == '{') { // I'm bad. :) program->structures[program->structure_count-1].path_count++; @@ -188,7 +192,9 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { program->structures[program->structure_count-1].paths[program->structures[program->structure_count-1].path_count-1].name = malloc(name_size*sizeof(char)); memcpy(program->structures[program->structure_count-1].paths[program->structures[program->structure_count-1].path_count-1].name, words[0], name_size); state = ktk_PARSE_PATH_DEFINITION; - } else if (words[0][0] == '}') { + } + } else { + if (words[0][0] == '}') { state = ktk_PARSE_STRUCTURE; } } @@ -207,7 +213,7 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { // **** end logic section **** // free our explode results int w; - for (w = 0; w < word_count; w++) { + for (w = 0; w <= word_count; w++) { free(words[w]); } free(words); @@ -223,6 +229,10 @@ int ktk_parseSFile(struct ktkProgram *program, const char *filename) { } } } + if (current_read != NULL) { + free(current_read); + current_read = NULL; + } fclose(file); return 0; } @@ -257,6 +267,7 @@ int ktk_parseSVars(struct ktkStructure *structure, const char **vars, size_t var } free(words[w]); } + free(words[w]); free(words); } else if (strcmp(vars[0], "x") == 0) { structure->x = ktk_parseNumber(vars[1]); @@ -303,6 +314,7 @@ int ktk_parseSRelation(struct ktkRelation *relation, const char **vars, size_t v } free(words[w]); } + free(words[w]); free(words); } else if (strcmp(vars[0], "x") == 0) { relation->x = ktk_parseNumber(vars[1]); @@ -344,6 +356,7 @@ int ktk_parseSPath(struct ktkPath *path, const char **vars, size_t var_count) { } free(words[w]); } + free(words[w]); free(words); } else if (strcmp(vars[0], "to") == 0) { size_t size = strlen(vars[1])+1; @@ -359,6 +372,7 @@ int ktk_parseSPath(struct ktkPath *path, const char **vars, size_t var_count) { } free(words[w]); } + free(words[w]); free(words); } else if (strcmp(vars[0], "to_x") == 0) { @@ -379,6 +393,7 @@ int ktk_parseSPath(struct ktkPath *path, const char **vars, size_t var_count) { } free(words[w]); } + free(words[w]); free(words); } else if (strcmp(vars[0], "from_x") == 0) { ktk_parseNumberSet(&path->from_x, vars[1]); diff --git a/src/main.c b/src/main.c index 8616fe8..ba889e7 100644 --- a/src/main.c +++ b/src/main.c @@ -43,6 +43,8 @@ int main(int argc, char *argv[]) { } ktk_deleteMap(&my_map); + ktk_deleteLive(&my_live); + ktk_deleteLive(&new_live); ktk_freeProgram(&my_program);