1

so I have been straggling pset5 for some time and I end up getting double free or corruptionand don't now how to fix it. Here's my code :

/* inside load() */
// some code
if(head->children[b] == NULL)
{
    node* n ;
    n = (node *)malloc(sizeof(node));

    for(int i = 0; i < 27; i++)
        n->children[i] = NULL;

    n->is_word=false;           
    head->children[b] = n;
    head = n;
    free(n);
}
// some code

bool unload(void)
{
    freenode(root);
    return true;
}

void freenode(node *n)
{
  for(int i = 0; i < 27; i++)
  {
        if(n->children[i] != NULL)
            freenode(n->children[i]);
        else
            break;
  }

  if(n != NULL)
    free(n);
}

Here's valgrind :

WORDS MISSPELLED:     7052
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        19190
TIME IN load:         3.49 
TIME IN check:        0.19
TIME IN size:         0.00
TIME IN unload:       0.00
TIME IN TOTAL:        3.69

==3086== 
==3086== HEAP SUMMARY:
==3086==     in use at exit: 0 bytes in 0 blocks
==3086==   total heap usage: 367,084 allocs, 367,260 frees, 82,227,504 bytes allocated
==3086== 
==3086== All heap blocks were freed -- no leaks are possible
==3086== 
==3086== For counts of detected and suppressed errors, rerun with: -v
==3086== ERROR SUMMARY: 2559314 errors from 15 contexts (suppressed: 0 from 0)
2

one block of memory can be pointed at by many pointers. this is exactly what happens when you allocate memory for one pointer, then set the value of another pointer to be the same as the first pointer's value. example

n = malloc(sizeof(node));
head = n;

this allocates a block of memory on the heap, sets n to point to that block, then sets head to point to the same block that n points to. so now both n and head point to the same block of memory allocated by the call to malloc.

according to the man page of free(), it frees the memory block pointed to by its argument. so when you call free on n, this also frees the memory block pointed to by head because it's actually the same block pointed to by n.

you then call free, passing in the same pointers that you think still point to memory blocks, so you get a double free error.

1

You are trying to free the same node twice. At the end of your freenode() function, you have the following code: if(n != NULL) free(n);. Remember that this is a recursive function. freenode() will recursively call itself until it gets to the bottom of the trie. Once there it frees all of the children at that level, and then the parent of the children that were just freed, but when the code returns to the parent of that call, or the next higher level of the recursion, that parent that was freed is now one of the children that it will attempt to free. That's the double free that you're encountering. If you remove that code from the end of the freenode() function, the problem gets resolved.

However, that leaves a minor issue. The root node will not be deleted. You will have to add code to delete the root node at the appropriate time.

This will fix the double free problem. No guarantees that you don't have other issues, but that wasn't the question. ;-)

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

You must log in to answer this question.

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