My program runs, but I am getting this message which I am fairly sure means that I have a memory leak and it is coming from my destructor.
Assignment2(779) malloc: *** error for object 0x100100120: pointer being freed was not allocated
I don't know what I am doing wrong. Since it is a binary search tree I figured while not empty go left if you can't go left go right, if you can't go right, delete. Any tips would be great.

BST::~BST()
{
	BinNodePtr parent = 0, temp = myRoot;
	while(!empty()){
		while(temp->left!='\0'){
			parent = temp;
			temp = temp->left;
		}
		 while (temp->right!='\0'){
			parent = temp;
			temp = temp->right;
		 }
			delete temp;
		}
}

Recommended Answers

All 5 Replies

Yea doesn't look like your destructing it properly. Try something like this :

BinarySearchTree::~BinarySearchTree(){
  _destroy(this._root);
}
void BinarySearchTree::_destroy(Node* root){
  if( isNull(root) ) return;
  else{
     _destroy(root.getLeft());
     _destroy(root.getRight());
      delete root;    
  }
}
commented: great solution +1

Thanks, my program no longer leaks memory. Do you have any ideas of what I could have done to my original destructor to make it work?

Thanks, my program no longer leaks memory. Do you have any ideas of what I could have done to my original destructor to make it work?

It looks like your trying to iteratively delete the nodes. Your algorithm does not transverse the entire tree, but it transverses all the way to the left, then maybe right,
and deletes that nodes, and exits. So to make your algorithm work, you would need to
modify how do the transversal, in which case, a recursive solution is easier to implement and understand.

malloc: *** error for object 0x100100120: incorrect checksum
*** set a breakpoint in malloc_error_break to debug
Abort

I come to this problem when I run a program,please someone help me!many thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.