0

Mem. leaking. help.

Only written load function but have the free function within load. Haven't fleshed out unload yet. Want to test that load works before fleshing out unload hence have included the free function in load.

valgrind says:

==1646== 
==1646== HEAP SUMMARY:
==1646==     in use at exit: 66,528 bytes in 11 blocks
==1646==   total heap usage: 14 allocs, 3 frees, 73,712 bytes allocated
==1646== 
==1646== 66,528 (6,048 direct, 60,480 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==1646==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1646==    by 0x4010F6: load (dictionary.c:77)
==1646==    by 0x40087D: main (speller.c:40)
==1646== 
==1646== LEAK SUMMARY:
==1646==    definitely lost: 6,048 bytes in 1 blocks
==1646==    indirectly lost: 60,480 bytes in 10 blocks
==1646==      possibly lost: 0 bytes in 0 blocks
==1646==    still reachable: 0 bytes in 0 blocks
==1646==         suppressed: 0 bytes in 0 blocks
==1646== 
==1646== For counts of detected and suppressed errors, rerun with: -v
==1646== ERROR SUMMARY: 12 errors from 2 contexts (suppressed: 0 from 0)

Helping with...                                                                                                                                      

==1646== 66,528 (6,048 direct, 60,480 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==1646==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1646==    by 0x4010F6: load (dictionary.c:77)
==1646==    by 0x40087D: main (speller.c:40)

Looks like your program leaked 66,528 bytes of memory. Did you forget to free memory that you allocated via malloc? Take a closer 
look at line 77 of dictionary.c.

Line 77 is: node *nodeP = malloc(MAX * sizeof(node)); below.

    /**
     * Loads dictionary into memory. Returns true if successful else false.
     */
    bool load(const char *dictionary)

{   
    int c;

//open dictionary file
FILE *dicp = fopen(dictionary, "r");
//check for Null value
if (dicp == NULL)
{
    printf("Could not open dictionary %s\n", dictionary);
    return 1;
}
//int a;

//pointer to the top of the trie
node *root = malloc(MAX * sizeof(node));
if(root == NULL)
    {
        return 2;
    }

travP = root;

//for every dictionary word, iterate through the trie
for(c = fgetc(dicp); c != EOF; c = fgetc(dicp))
{
    //if LF is hit, set word to true

    if (isalpha(c) || (c == '\''))
    { 
        //Each element in children corresponds to a different letter
        //convert a to the equivalent ascii value which will serve as the index 'c' in the array children
        c = c - 97;

        //printf("%c\n", c);

        //check if children[c] is null  
        if (travP->children[c] == NULL)
        {
            //if children[c]== NULL, malloc a new node.
            node *nodeP = malloc(MAX * sizeof(node));

            //have children[c] point to the new node
            travP->children[c] = nodeP; //Is this correct?

            //go down to that block of memory
            travP = travP->children[c];

            //I might need a recursive function. 

        }

        else
        {
            //if not NULL move to new node and continue
            travP = travP->children[c];
        }
    }

    //if at end of word, set is_word to true
    else
    {
        travP->is_word = 1;
        travP = root;
    }



}

//Free all malloced memory

free(nodeP);
free(root);

fclose(dicp);

//return true if load was successful
if(c == EOF)
    return true;

//return false if load was unsuccessful
else
    return false;
1

If you free the root node in load, you cannot use it in check. You allocate "lots of " child nodes, but only "free" the last one. You absolutely can test that load works before implementing unload, and I daresay you should. (NB the best way to test that load works, is to write check).

Do all the freeing in unload. Remember, each node's address is stored in an array somewhere, so each node does not need a "name". You'll need to iterate through those arrays (starting with root), to free each node.

There's another problem brewing: ascii apostrophe is 39. If the character is apostrophe, what will happen here c = c - 97;?

| improve this answer | |
  • @DinoCodeSaurus Thank you! I thought that I would need to free the rest of the nodes. I'm at a loss as to how to free every other node without naming each of them, which leads to the question of how to name all the nodes? – Salamit Mar 25 '17 at 6:00
  • 1
    Info added to answer. I hope it helps. – DinoCoderSaurus Mar 25 '17 at 12:31
  • Thank you! Just getting to this. Will follow up after implementing what you mentioned. – Salamit Apr 4 '17 at 3:29

You must log in to answer this question.

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