0

I'm running valgrind and it's telling me that I need to free something and I'm not sure where I would do that. It specifically points to this line "toInsert->value = linkedlist->copy(element);

Where would I run free and on what data?

"

void linkedlist_append(LinkedList *linkedlist, void *element) {

ListNode *toInsert = (ListNode*)malloc(sizeof(ListNode));
toInsert->value = linkedlist->copy(element);
toInsert->next = NULL;
ListNode *last = linkedlist->head;

if (last==NULL)
    linkedlist->head  = toInsert;

else{
    while(last-> next !=NULL){
        last = last->next;

    }
    last->next = toInsert;

}
linkedlist->size++;

}

Output from Valgrind:

> ^C==30515== 
==30515== HEAP SUMMARY:
==30515==     in use at exit: 287 bytes in 47 blocks
==30515==   total heap usage: 95 allocs, 1,038 frees, 2,159 bytes allocated
==30515== 
==30515== 247 bytes in 46 blocks are definitely lost in loss record 2 of 2
==30515==    at 0x4C28C20: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30515==    by 0x4011AC: string_copy (string_fns.c:27)
==30515==    by 0x4012CE: linkedlist_append (linkedlist.c:87)
==30515==    by 0x4017BD: add_file (tester.c:194)
==30515==    by 0x400FE1: main (tester.c:136)
==30515== 
==30515== LEAK SUMMARY:
==30515==    definitely lost: 247 bytes in 46 blocks
==30515==    indirectly lost: 0 bytes in 0 blocks
==30515==      possibly lost: 0 bytes in 0 blocks
==30515==    still reachable: 40 bytes in 1 blocks
==30515==         suppressed: 0 bytes in 0 blocks
==30515== Reachable blocks (those to which a pointer was found) are not shown.
==30515== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==30515== 
==30515== For counts of detected and suppressed errors, rerun with: -v
==30515== ERROR SUMMARY: 2200 errors from 10 contexts (suppressed: 0 from 0)
Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
learner561
  • 127
  • 2
  • 11
  • Usually, valgrind tells you the variable it's finding problems with. Can you show us the output from valgrind? –  Nov 13 '16 at 08:06
  • 3
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Nov 13 '16 at 08:06
  • Given that you `malloc`ed `toInsert`, that's probably the variable you want to free as well. –  Nov 13 '16 at 08:07
  • What is `linkedlist->copy` anyway? What's the definition of `LinkedList`? –  Nov 13 '16 at 08:08
  • @Evert this is a snippet from a linkedlist implementation. LinkedList->copy is a function that copies the element and returns a pointer to that copy – learner561 Nov 13 '16 at 08:11
  • If you have `copy` function pointer, your probably need to have and call a `release` function pointer. (P.S. ADTs in c are a pain to write) – StoryTeller - Unslander Monica Nov 13 '16 at 08:12
  • Valgrind points to a line with a function, yet you don't show the function. That makes it hard to state what's causing the problem. –  Nov 13 '16 at 08:13
  • There's not enough information here to help you: please try and reduce your problem to an [SSCCE](http://sscce.org/). –  Nov 13 '16 at 08:16
  • @Evert I posted the linkedlist_append function above if that's whats being referred to. I unfortunately am not given access to the other files/functions :/ – learner561 Nov 13 '16 at 08:17
  • How is the new node you added being destroyed? The problem is most likely in *that* function. – StoryTeller - Unslander Monica Nov 13 '16 at 08:20
  • @StoryTeller I'm wondering why the node I added would need to be destroyed? Wouldn't that negate the appending – learner561 Nov 13 '16 at 08:23
  • Of course it would to be destroyed eventually. When the list is no longer needed, it should release all resources it holds. And that includes the memory of individual nodes... I'm not saying it should be destroyed in `list_append` – StoryTeller - Unslander Monica Nov 13 '16 at 08:25
  • @StoryTeller Since valgrind mentions the memory leak occurs in list_append is it possible that the leak propagates down to function I used to delete nodes in the list? – learner561 Nov 13 '16 at 08:30
  • Not only possible, but most likely. valgrind points to where the memory that leaks is being allocated. Now that you know what it is, you need to check you release it properly. – StoryTeller - Unslander Monica Nov 13 '16 at 08:31
  • 1
    Please don't edit out the code that people already referenced. Rolled back the edit. – Ingo Bürk Nov 13 '16 at 08:45

1 Answers1

3

Valgrind is pointing you to the location of a malloc that is never freed. Before you terminate your program, you will need to iterate through the list an free all its elements.

doron
  • 27,972
  • 12
  • 65
  • 103