My code passes check50 apart from the memory leak section. Valgrind shows no leaks are possible but lots of errors:
==21401== HEAP SUMMARY:
==21401== in use at exit: 0 bytes in 0 blocks
==21401== total heap usage: 143,096 allocs, 143,096 frees, 8,023,416 bytes allocated
==21401==
==21401== All heap blocks were freed -- no leaks are possible
==21401==
==21401== For counts of detected and suppressed errors, rerun with: -v
==21401== ERROR SUMMARY: 4513263 errors from 4 contexts (suppressed: 0 from 0)
Then if I rerun with -v it shows:
Could not open -v.
==21531==
==21531== HEAP SUMMARY:
==21531== in use at exit: 6,974,856 bytes in 124,551 blocks
==21531== total heap usage: 124,580 allocs, 29 frees, 6,982,480 bytes allocated
==21531==
==21531== 6,974,856 bytes in 124,551 blocks are still reachable in loss record 1 of 1
==21531== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21531== by 0x401385: load (dictionary.c:128)
==21531== by 0x4009F4: main (speller.c:40)
==21531==
==21531== LEAK SUMMARY:
==21531== definitely lost: 0 bytes in 0 blocks
==21531== indirectly lost: 0 bytes in 0 blocks
==21531== possibly lost: 0 bytes in 0 blocks
==21531== still reachable: 6,974,856 bytes in 124,551 blocks
==21531== suppressed: 0 bytes in 0 blocks
==21531==
==21531== For counts of detected and suppressed errors, rerun with: -v
==21531== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
On line 128 where it is pointing out an error I have:
node *n = malloc(sizeof(node));
This code is what I am using to assign some memory to every new word from the dictionary. I tried using free(n) but it causes a segmentation fault. Anyone know what is causing the leak and why?
Edit (to include unload function): Edit 2 (removed some of unload code for academic reasons):
//added if statements to free last node from list. Not sure if they're needed
if (head == NULL)
{
free(head);
return true;
}
if (cursor == NULL)
{
free(head);
return true;
}
while (cursor != NULL)
//code to move cursor along and free head.
return true;