1

My PSET "mispelling" works fine and contains no memory leak but when I run the program with Valgrind it says Invalid read of size 4.

My unload function calls a deleteTrie function which looks like this:

        //calling from unload  
        deleteTrie(root, 0);  

        void deleteTrie(node *cursor, int i)
        {
        while(i < 28)
        {
                if(cursor->children[i] != NULL)
                {
                        deleteTrie(cursor->children[i], 0);
                }

                i++;
        }
        free(cursor);
}

Valgrind shows the following errors:

==3403== Invalid read of size 4  
==3403==    at 0x80493C5: deleteTrie (dictionary.c:125)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049383: unload (dictionary.c:108)  
==3403==    by 0x8048D04: main (speller.c:157)  
==3403==  Address 0x41ff4c8 is 0 bytes after a block of size 136 alloc'd  
==3403==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
==3403==    by 0x804923D: load (dictionary.c:75)  
==3403==    by 0x8048775: main (speller.c:45)  
==3403==   
==3403== Invalid read of size 4  
==3403==    at 0x80493C5: deleteTrie (dictionary.c:125)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049383: unload (dictionary.c:108)  
==3403==    by 0x8048D04: main (speller.c:157)  
==3403==  Address 0x41ff410 is 0 bytes after a block of size 136 alloc'd  
==3403==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
==3403==    by 0x804923D: load (dictionary.c:75)  
==3403==    by 0x8048775: main (speller.c:45)  
==3403==   
==3403== Invalid read of size 4  
==3403==    at 0x80493C5: deleteTrie (dictionary.c:125)  
==3403==    by 0x8049411: deleteTrie (dictionary.c:134)  
==3403==    by 0x8049383: unload (dictionary.c:108)  
==3403==    by 0x8048D04: main (speller.c:157)  
==3403==  Address 0x41ff358 is 0 bytes after a block of size 136 alloc'd  
==3403==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
==3403==    by 0x804923D: load (dictionary.c:75)  
==3403==    by 0x8048775: main (speller.c:45)  
==3403== 
==3403== Invalid read of size 4  
==3403==    at 0x80493C5: deleteTrie (dictionary.c:125)  
==3403==    by 0x8049383: unload (dictionary.c:108)  
==3403==    by 0x8048D04: main (speller.c:157)  
==3403==  Address 0x41ff2a0 is 0 bytes after a block of size 136 alloc'd  
==3403==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
==3403==    by 0x8049145: load (dictionary.c:62)  
==3403==    by 0x8048775: main (speller.c:45)  

Here is the HEAP summary:

==3403== HEAP SUMMARY:  
==3403==     in use at exit: 0 bytes in 0 blocks  
==3403==   total heap usage: 261,371 allocs, 261,371 frees, 35,546,798 bytes allocated  
==3403==   
==3403== All heap blocks were freed -- no leaks are possible  
==3403==   
==3403== For counts of detected and suppressed errors, rerun with: -v  
==3403== ERROR SUMMARY: 261368 errors from 4 contexts (suppressed: 0 from 0)  
1
  • 1
    26 letters in the alphabet plus an apostrophe = 27. You are running a loop i < 28. Could this be it? Is it running through too many times? Depends what you initialize i to be (0 or 1).
    – lethaljd
    Nov 24 '14 at 15:17

You must log in to answer this question.

Browse other questions tagged .