0

UPDATE:

Fixed the lack of free() in my destroy() function. But still got 244 bytes unfree'd memory.

valgrind result:

    ==787136== 
    ==787136== HEAP SUMMARY:
    ==787136==     in use at exit: 224 bytes in 1 blocks
    ==787136==   total heap usage: 350,242 allocs, 350,241 frees, 78,454,896                         
    bytes allocated
    ==787136== 
    ==787136== 224 bytes in 1 blocks are still reachable in loss record 1 of 1
    ==787136==    at 0x4C2CC70: calloc (in         
    /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==787136==    by 0x401230: load (dictionary.c:99)
    ==787136==    by 0x4008BD: main (speller.c:46)
    ==787136== 
    ==787136== LEAK SUMMARY:
    ==787136==    definitely lost: 0 bytes in 0 blocks
    ==787136==    indirectly lost: 0 bytes in 0 blocks
    ==787136==      possibly lost: 0 bytes in 0 blocks
    ==787136==    still reachable: 224 bytes in 1 blocks
    ==787136==         suppressed: 0 bytes in 0 blocks
    ==787136== 
    ==787136== For counts of detected and suppressed errors, rerun with: -v
    ==787136== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

**********************************BEFORE UPDATE*****************************

GDB gives "Child terminated with signal = 0xb (SIGSEGV)", no idea what it means.

GDB seg-fault 'ed when the recursion function is called in unload(). It stopped at the first line of recursion function, which doesn't even has code.

Any help will be appreciated.

My unload() and called recursion function:

bool unload(void)
{
    for (int i = 0; i < 27; i++)
    {
        destroy(root->children[i]);
    }
    destroy(root);
    return true;
}

void destroy(NODE* parent)
{ // this is where GDB stopped with seg fault
    if (!parent) // ensure the parent actually has children -> not pointing NULL
    return;
    for (int i = 0; i < 27; i++)
    {
        destroy(parent->children[i]); // recusively call destroy() 
        // go down the tries to the very end where all children of 
        // a node point to NULL so that this node can be free
    }    
    free(parent); // free the parent node whose children all point to NULL
                  // FIXED: this was used to be destroy(parent);
}

Below is valgrind result:

    ==760567== Stack overflow in thread 1: can't grow stack to 0xffe801ff8
    ==760567== 
    ==760567== Process terminating with default action of signal 11 (SIGSEGV)
    ==760567==  Access not within mapped region at address 0xFFE801FF8
    ==760567==    at 0x401330: destroy (dictionary.c:149)
    ==760567==  If you believe this happened as a result of a stack
    ==760567==  overflow in your program's main thread (unlikely but
    ==760567==  possible), you can try to increase the size of the
    ==760567==  main thread stack using the --main-stacksize= flag.
    ==760567==  The main thread stack size used in this run was 8388608.
    ==760567== Stack overflow in thread 1: can't grow stack to 0xffe801ff0
    ==760567== 
    ==760567== Process terminating with default action of signal 11 (SIGSEGV)
    ==760567==  Access not within mapped region at address 0xFFE801FF0
    ==760567==    at 0x4A256B0: _vgnU_freeres (in         

    /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
    ==760567==  If you believe this happened as a result of a stack
    ==760567==  overflow in your program's main thread (unlikely but
    ==760567==  possible), you can try to increase the size of the
    ==760567==  main thread stack using the --main-stacksize= flag.
    ==760567==  The main thread stack size used in this run was 8388608.
    ==760567== 
    ==760567== HEAP SUMMARY:
    ==760567==     in use at exit: 78,453,760 bytes in 350,240 blocks
    ==760567==   total heap usage: 350,242 allocs, 2 frees, 78,454,896 bytes         

    allocated
    ==760567== 
    ==760567== LEAK SUMMARY:
    ==760567==    definitely lost: 0 bytes in 0 blocks
    ==760567==    indirectly lost: 0 bytes in 0 blocks
    ==760567==      possibly lost: 0 bytes in 0 blocks
    ==760567==    still reachable: 78,453,760 bytes in 350,240 blocks
    ==760567==         suppressed: 0 bytes in 0 blocks
    ==760567== Rerun with --leak-check=full to see details of leaked memory
    ==760567== 
    ==760567== For counts of detected and suppressed errors, rerun with: -v
    ==760567== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    Segmentation fault
3
  • 1) Don't you need to free something somewhere? and 2) your recursive fn could be running out of stack memory as per this. Sep 8 '16 at 12:48
  • Yep, thanks! This one is now fixed, but still figuring out why there is still reachable 244 bytes in 1 block not free'd. Sep 8 '16 at 17:46
  • Here's a good hint from good ol' valgrind. Rerun with --leak-check=full to see details of leaked memory. My goto call to valgrind is valgrind -v --leak-check=full --show-leak-kinds=all --track-origins=yes to get maximum feedback. Sep 8 '16 at 22:02
1

@DinoCoderSaurus called it in the comments. Your destroy() function is calling itself recursively, and the comments indicate that you intend to free heap memory somewhere in there. Yet there is no actual call to free().

You must log in to answer this question.

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