Question:
valgrind is telling me that a specific line in my code creates a memory leak, but when looking at that line, it does not seem to even be able to create one.I am working with this pretty simple Linked List struct list.h:
Now here is where the memory leak occurs:
Answer:
You don’t free the last node in the list.free_list
does nothing if list->next
is NULL
. But you don’t want to do nothing. You want to not recurse, but you still need to free the node. So move the call to free
out of the conditional, or change the test to check whether list
itself is NULL
If you have better answer, please add a comment about this, thank you!