fixed a memory leak

This commit is contained in:
overflowerror 2019-03-03 12:11:44 +01:00
parent 08743ef960
commit 7dfa80572d
3 changed files with 13 additions and 5 deletions

View file

@ -9,7 +9,8 @@ all: $(BIN_NAME)
test: obj/test.o obj/networking.o obj/linked.o test: obj/test.o obj/networking.o obj/linked.o
$(LD) $(LDFLAGS) -o $@ $^ $(LD) $(LDFLAGS) -o $@ $^
valgrind: test valgrind: CFLAGS += -static -g
valgrind: clean test
valgrind --leak-check=yes ./test valgrind --leak-check=yes ./test
obj/test.o: src/networking.h obj/test.o: src/networking.h

View file

@ -73,18 +73,19 @@ void linked_free(link_t* link) {
void linked_release(link_t* link) { void linked_release(link_t* link) {
link->inUse--; link->inUse--;
if (link->unlinked && link->inUse == 0) { if (link->unlinked && (link->inUse == 0)) {
linked_free(link); linked_free(link);
} }
} }
link_t* linked_next(link_t* link) { link_t* linked_next(link_t* link) {
link_t* next = link->next;
linked_release(link); linked_release(link);
if (link->next == NULL) { if (next == NULL) {
return NULL; return NULL;
} }
link = link->next; link = next;
link->inUse++; link->inUse++;
@ -194,7 +195,7 @@ int linked_unlink(link_t* link) {
} }
void linked_destroy(linkedList_t* list) { void linked_destroy(linkedList_t* list) {
link_t* link = linked_first(list); link_t* link = list->first;
while(link != NULL) { while(link != NULL) {
link_t* next = link->next; link_t* next = link->next;

View file

@ -75,6 +75,12 @@ void testLinkedList() {
checkInt(linked_length(&list), 2, "list length"); checkInt(linked_length(&list), 2, "list length");
link = list.first;
while(link != NULL) {
checkInt(link->inUse, 0, "raw inUse");
link = link->next;
}
linked_destroy(&list); linked_destroy(&list);
} }