0

I'm working on my memory leaks for the load() function. Valgrind give me the following error message

==1675== HEAP SUMMARY:
==1675==     in use at exit: 8,011,640 bytes in 143,065 blocks
==1675==   total heap usage: 143,096 allocs, 31 frees, 8,023,416 bytes allocated
==1675== 
==1675== 8,010,184 bytes in 143,039 blocks are indirectly lost in loss record 1 of 2
==1675==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1675==    by 0x401136: load (dictionary.c:89)
==1675==    by 0x400964: main (speller.c:40)
==1675== 
==1675== 8,011,640 (1,456 direct, 8,010,184 indirect) bytes in 26 blocks are definitely lost in loss record 2 of 2
==1675==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

I'm trying to unload the used memory. I've tried to set new_node = null as you see in the code below, but that did not solve it. My unload() function works without error, but does not manage to deal with the memory leaks. Any additional hints would be greatly appreciated.

My load function, which is getting error is below. The line referenced in the error message above is:

new_node = (struct node *) calloc(1, sizeof(node));

while (fscanf(file, "%s", word) != EOF)
    {
        // TODO

        //NOW:
        countWords++;
        //create node
        struct node * new_node;

        //create a new node anytime new word
        //create a node and allocate memory for each word

       // a = (int*)calloc(n, sizeof(int));
       //new_node = (struct node *) malloc( sizeof(node));
         new_node = (struct node *) calloc(1, sizeof(node));
        //if empty...
        if (new_node == NULL)
        {
            //speller quits
            unload();
            return false;
        }

        //copy word into node
        strcpy(new_node->word, word);

        //get location of word to go in hashtable by hashing value
        int index = hash(word);

        //put word in that location if nothing there
        if(hashtable[index] == NULL)
        {
             new_node->next = NULL;
            //QUESTION: is head in array or at end of linked-list?
            hashtable[index] = new_node;

        }

        //we need to link it to last node
        else
        {
          // struct node * head = (struct node *) malloc(sizeof(node));
           head = hashtable[index];
           new_node -> next = head->next; //I may not be assigning this correct
           head->next = new_node;

        }
        new_node = NULL;
    }
    //printNumber();//header);
   // free(new_node);
    // Close dictionary

    fclose(file);

    // Indicate success
    return true;
}
0

Setting new_node to NULL in load immediately throws away your node!

Your unload function is how you free the memory that you allocated.

1
  • Ok thanks! Just curious if I set it to NULL at that point in code I thought it would be ok since I remake the node each time in the while loop ... But I might be running into an error here in my logic – Katie Melosto Jan 12 '20 at 20:28

You must log in to answer this question.

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