0

I have completed the code for pset5 but I have a super big memory leak which i believe comes from not pointing each backed in the hash_table correctly. Below I have unload function and valgrind's results. If anyone can help me by pointing what's wrong with the unload function that would be great!! Thanks in advance.

load part that seems to create the problem in line 95:

while (fscanf(fp,"%s", word_c)!=EOF)
{
    node *new_node=malloc(sizeof(node));
    strcpy(new_node->word,word_c);
    int h=hash_function(word_c);
    ++word_count; 

bool unload(void)

{

node *cursor=NULL;

for (int i=0; i<SIZE; i++)

{

    cursor=hash_table[i];

    while (cursor!=NULL)

    {

        node *temp=cursor;

        cursor=cursor->next;

        free(temp); 
    }
}

return true;

}

Valgrind result:

==5342==

==5342== HEAP SUMMARY:

==5342== in use at exit: 8,011,640 bytes in 143,065 blocks

==5342== total heap usage: 143,093 allocs, 28 frees, 8,014,232 bytes allocated

==5342==

==5342== 8,011,640 bytes in 143,065 blocks are definitely lost in loss record 1 of 1

==5342== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==5342== by 0x401469: load (dictionary.c:95)

==5342== by 0x400A9D: main (speller.c:45)

==5342==

==5342== LEAK SUMMARY:

==5342== definitely lost: 8,011,640 bytes in 143,065 blocks

==5342== indirectly lost: 0 bytes in 0 blocks

==5342== possibly lost: 0 bytes in 0 blocks

==5342== still reachable: 0 bytes in 0 blocks

==5342== suppressed: 0 bytes in 0 blocks

==5342==

==5342== For counts of detected and suppressed errors, rerun with: -v

==5342== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

1
  • I see that you're creating and populating new_node in a while loop, but are you adding the new_node to a linked list before looping back through the loop?
    – Cliff B
    Feb 6, 2016 at 21:28

1 Answer 1

0

It's almost impossible to tell what's going on from the limited amount of code that is posted. However, given that you're leaking about 8 MB and valgrind isn't happy with something in the while loop, I'll make a guess.

Are you actually adding the new_node that you are creating to the tree? If I have to guess, I'm thinking no, so you're losing all of the malloc'd nodes as you process the dictionary. If this is not the case, then there's no way to tell what's happening without seeing more code.

Also, you don't indicate whether the code is actually working. Is it? If I am right in what I said earlier, my guess is that the program is far from working correctly. If the code isn't working, then trying to fix memory leaks is usually premature. It might help diagnose a problem, but more often than not, debugging a program will cure a lot of memory leaks. My general advice is to get it working and then worry about memory leaks.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks Cliff. Actually I found the solution and fixed valgrind results. There were two mistakes. The first was that I hadn't declared word a pointer to a char but instead an array and secondly I wasn't pointing the cursor pointer to the beginning of the linked list.
    – dean.d
    Feb 20, 2016 at 18:13

You must log in to answer this question.

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