I'm using a trie data structure to tackle the spell checker. The code works fine and is almost as fast as the staff's implementation. Even check50 is happy about it.

But when I run valgrind it throws up this stuff:

==2707== HEAP SUMMARY:
==2707==     in use at exit: 84,835,408 bytes in 757,459 blocks
==2707==   total heap usage: 757,462 allocs, 3 frees, 84,836,224 bytes allocated
==2707== LEAK SUMMARY:
==2707==    definitely lost: 0 bytes in 0 blocks
==2707==    indirectly lost: 0 bytes in 0 blocks
==2707==      possibly lost: 0 bytes in 0 blocks
==2707==    still reachable: 84,835,408 bytes in 757,459 blocks
==2707==         suppressed: 0 bytes in 0 blocks
==2707== Reachable blocks (those to which a pointer was found) are not shown.
==2707== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2707== For counts of detected and suppressed errors, rerun with: -v
==2707== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Here is my source code: https://gist.github.com/anonymous/669d315165110705acb8

Can anyone point out what's wrong?

---------- UPDATE ----------

Here is my updated working code of the freeMem function:

bool freeMem(node* pntr)
    // free each not null pointer
    for (int i = 0; i < MAX; i++)
        if (pntr->next[i] != NULL)

    // free the node itself

    return true;

Figured it out! The problem was on dictionary.c:179. I replaced return freeMem(pntr->next[i]) with freeMem(pntr->next[i]) and now valgrind is happy!


"still reachable" means that those blocks of memory can still be reached (meaning, they still have a pointer pointing towards them), but your unload function hasn't freed them.

My guess is that your freemem function is returning when it shouldn't. Keep in mind that a function doesn't do anything after a return statement. Yes, it will call itself, but all the lines after the return are never executed so your function unwinds when it hits NULL and never frees anything.

freemem doesn't need to be bool. It doesn't really need to return anything. You would need a base case (when is your function meant to return? That's your base case, so if (base case is met) return; ) And then, you simply call your function as you're doing now, and freeing when you're freeing.

  • I changed the freeMem function to a type void and deleted the return statements. But still valgrind yells the same! Sep 13 '15 at 12:01
  • Can you post your updated freemem function?
    – Irene
    Sep 13 '15 at 18:45
  • I figured it out ;-) Check the updated code in the question. Sep 14 '15 at 7:41

You must log in to answer this question.

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