0

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;
1
  • can you post the whole dictionary.c? – stensal May 29 '20 at 16:19
2

Valgrind is telling you where the memory in question is being allocated, not where the problem lies.

When valgrind says that memory is still reachable, its really saying that it wasn't freed before the program ended. That's very different from a leak. Technically, the memory would be cleaned up by the system when the program ends, but it's best practice for a program to clean up all allocated memory before exiting.

Somewhere, the memory is not being freed before the program ends. Without seeing any code, it's just a guess, but I'd say that unload is not working correctly.

Also, you don't want to free anything in load because it's going to be used throughout the rest of the program.

[EDIT - after seeing unload] Yup, definitely unload.

See those if(... == NULL) code blocks? As soon as one is true, the return statements are terminating unload - before the remaining nodes in the tree are freed. Get rid of those premature return statements. You need to traverse the entire tree before executing a return true.

Programming tip: If a pointer points to NULL, there's nothing to free, so free(NULL), which is effectively what that code does, serves no purpose. In this code, both of those code blocks can be totally removed.

1
  • Thanks for replying. I've added the unload function to the post. I'm using the debugger and it looks like the cursor is moving along the list fine. Is it to do with the order I have put the lines of code in? – hh1265 May 28 '20 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .