Monty Swanson
Monty Swanson

Reputation: 795

*** glibc detected *** ./a.out: double free or corruption (top): 0x08901d70 *** while attempting to free a BST

I do realize there are some "glibc detected" posts but I would be very grateful if you could suggest a solution for this:

*** glibc detected *** ./a.out: double free or corruption (top): 0x08901d70 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x17c501]
/lib/libc.so.6(+0x6dd70)[0x17dd70]
/lib/libc.so.6(cfree+0x6d)[0x180e5d]
/lib/libc.so.6(fclose+0x14a)[0x16c81a]
./a.out[0x8048998]
/lib/libpthread.so.0(+0x5cc9)[0xc1fcc9]
/lib/libc.so.6(clone+0x5e)[0x1e069e]
======= Memory map: ========

This seems to happen when I attempt to free a binary search tree:

void freetree(BNODEPTR *root)
{
        if(root!=NULL)
        {
                freetree(root->left);
                freetree(root->right);
                free(root);
        }
}  

The structure is typedef'd to BNODEPTR

struct bnode{
        int info;
        int count;
        struct bnode* left;
        struct bnode* right;
};

I am calling the function from main() using freetree(root).

The tree seems to be implemented correctly as an inorder traversal produces a sorted output.

The entire code is at:

http://pastebin.com/Eieu3xDa and

http://pastebin.com/jtGN6XKj

Upvotes: 0

Views: 5152

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213508

I could sift through your source code, but as they say, "Feed a man a fish..."

  1. Compile your code with debugging symbols (pass -g to the compiler). If you do this, you can get a function name instead of ./a.out[0x8048998] in the backtrace.

  2. Run your code with Valgrind's memcheck tool (the default tool). This might give you a much better clue about where the error is. You can just install Valgrind and run valgrind ./a.out for starters.

In particular, I think the whole binary tree is a red herring. There is another problem in your program somewhere else. From the backtrace, I can see that (1) the error message is not triggered in freetree and (2) you are using threads, which are easily misused.

Upvotes: 3

Related Questions