diff --git a/Makefile b/Makefile index 21020a1..c7d07a2 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,8 @@ all: $(BIN_NAME) test: obj/test.o obj/networking.o obj/linked.o $(LD) $(LDFLAGS) -o $@ $^ -valgrind: test +valgrind: CFLAGS += -static -g +valgrind: clean test valgrind --leak-check=yes ./test obj/test.o: src/networking.h diff --git a/src/linked.c b/src/linked.c index 7e725af..d4f8b2d 100644 --- a/src/linked.c +++ b/src/linked.c @@ -73,18 +73,19 @@ void linked_free(link_t* link) { void linked_release(link_t* link) { link->inUse--; - if (link->unlinked && link->inUse == 0) { + if (link->unlinked && (link->inUse == 0)) { linked_free(link); } } link_t* linked_next(link_t* link) { + link_t* next = link->next; linked_release(link); - if (link->next == NULL) { + if (next == NULL) { return NULL; } - link = link->next; + link = next; link->inUse++; @@ -194,7 +195,7 @@ int linked_unlink(link_t* link) { } void linked_destroy(linkedList_t* list) { - link_t* link = linked_first(list); + link_t* link = list->first; while(link != NULL) { link_t* next = link->next; diff --git a/src/test.c b/src/test.c index ce7072e..6f4db2a 100644 --- a/src/test.c +++ b/src/test.c @@ -75,6 +75,12 @@ void testLinkedList() { 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); }