C++ error: Pointer being freed was not allocated
<p>I'm building an AVL tree. I have a method to delete items in the tree, but I'm getting an error.</p>

<p>This is the run-time error I get: </p>

<pre><code>malloc: *** error for object 0x100100120: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
jim Abort trap
</code></pre>

<p>My class looks like this:</p>

<pre><code>struct Avlnode{
string data;
int balfact;
Avlnode *left;
Avlnode *right;
};

class Avltree{
public:
Avltree();
~Avltree( );
Avlnode* insert(string i, bool* j);
static Avlnode* buildtree ( Avlnode *root, string data, bool *h ) ;
void display( Avlnode *root );
Avlnode* deldata ( Avlnode* root, string data, bool *h );
static Avlnode* del ( Avlnode *node, Avlnode* root, bool *h );
static Avlnode* balright ( Avlnode *root, bool *h );
static Avlnode* balleft ( Avlnode* root, bool *h );
void setroot ( Avlnode *avl );
static void deltree ( Avlnode *root );
private:
Avlnode *root;
int items;
};
</code></pre>

<p>and the deldata is defined like this:</p>

<pre><code>Avlnode* Avltree::deldata ( Avlnode *root, string data, bool *h ){
Avlnode *node;

if ( root == NULL ){
    //cout &lt;&lt; "\nNo such data.";
    return ( root );
}
else{
    if ( data &lt; root -&gt; data ){
        root -&gt; left = deldata ( root -&gt; left, data, h ) ;
        if ( *h )
            root = balright ( root, h ) ;
    }
    else{
        if ( data &gt; root -&gt; data ){
            root -&gt; right = deldata ( root -&gt; right, data, h ) ;
            if ( *h )
                root = balleft ( root, h );
        }
        else{
            node = root;
            if ( node -&gt; right == NULL ){
                root = node -&gt; left ;
                *h = true ;
                delete ( node ) ;
            }
            else{
                if ( node -&gt; left == NULL ){
                    root = node -&gt; right ;
                    *h = true ;
                    delete ( node ) ;
                }
                else{
                    node -&gt; right = del ( node -&gt; right, node, h ) ;
                    if ( *h )
                        root = balleft ( root, h ) ;
                }
            }
        }
    }
}
return ( root ) ;
}

Avlnode* Avltree :: del ( Avlnode *succ, Avlnode *node, bool *h ){
Avlnode *temp = succ ;

if ( succ -&gt; left != NULL ){
    succ -&gt; left = del ( succ -&gt; left, node, h ) ;
    if ( *h )
        succ = balright ( succ, h ) ;
}
else{
    temp = succ ;
    node -&gt; data = succ -&gt; data ;
    succ = succ -&gt; right ;
    delete ( temp ) ;
    *h = true ;
}
return ( succ ) ;
}
</code></pre>

<p>Why am I getting this error?</p>
