title: *** glibc detected *** ./a.out: double free or corruption (top): 0x08901d70 *** while attempting to free a BST
body: <p>I do realize there are some "glibc detected" posts but I would be very grateful if you could suggest a solution for this:</p>
  
  <pre><code>*** 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: ========
  </code></pre>
  
  <p>This seems to happen when I attempt to free a binary search tree:</p>
  
  <pre><code>void freetree(BNODEPTR *root)
  {
          if(root!=NULL)
          {
                  freetree(root-&gt;left);
                  freetree(root-&gt;right);
                  free(root);
          }
  }  
  </code></pre>
  
  <p>The structure is typedef'd to BNODEPTR</p>
  
  <pre><code>struct bnode{
          int info;
          int count;
          struct bnode* left;
          struct bnode* right;
  };
  </code></pre>
  
  <p>I am calling the function from main() using freetree(root).</p>
  
  <p>The tree seems to be implemented correctly as an inorder traversal produces a sorted output.</p>
  
  <p>The entire code is at:</p>
  
  <p><a href="http://pastebin.com/Eieu3xDa" rel="nofollow">http://pastebin.com/Eieu3xDa</a>  and</p>
  
  <p><a href="http://pastebin.com/jtGN6XKj" rel="nofollow">http://pastebin.com/jtGN6XKj</a></p>