Debugging a delete error?

I am pretty sure I have some sort of deletion error. How would I go about debugging it? I am still not savvy with using the call stack and finding out what it can tell me. My program runs fine (or the output is correct) but at the very end it still puts out an error from a delete_scalar.cpp file.

"A breakpoint instruction (__debugbreak() statement or a similar call) was executed in Project x.exe."
I don't know if I want to post my whole code, but I did use Dr. memory and it said I had 0 leaks. Most likely the last delete function is causing the error.

**a friend had me add a message when the destructor happens, and I test for it once but it's printing out 3 times.
Last edited on
The delete error is most likely that you are trying to free memory more than once. This happens when a shallow (vs. deep) copy is performed, i.e. when the pointer instead of the whole data is copied.

This can be prevented by using smart pointer (std::shared_ptr or std::unique_ptr) or a container. Depends on your needs. You should avoid using raw pointer when possible.
Assuming the struct/class uses dynamic memory, have you defined a correct copy constructor and an assignment operator (operator=)? If you haven't then the default ones supplied are not appropriate as they do a shallow copy rather than the required deep copy.

As you haven't provided the code, we can't say much else without seeing it as this could be caused in several ways depending upon which the code is trying to do. With C++, just because the code gives a semblance of working this doesn't mean it's working correctly.
while it won't fix errors in your code, if you set pointers to nullptr every time you delete them and every time you initialize one (unless you init to a valid pointer) you will have safe code, even if you blunder and delete twice, because deletion of nullptr does nothing & is safe, you just waste some time doing nothing there.

Its best if you figure this out so its not called more than once, and understand how to make that work, but its also best if you do the above, just for safer, more goof-proofed code.

** this won't fix your issue if the issue is that you copied the pointer to another and deleted both.
Last edited on
It is a possibility that you have overwritten some block of memory snd corrupted the heap. If that is the case, it might show up when delete is called.
Last edited on
@jonnin yes I had realized I might need to add some nullptrs in my code, and i probably will run into the second thing you mentioned since this is a bit complicated a nullptr might not fix it. So I will try and post some code soon.
Ok I am still getting 3 destructor errors, I probably got bad advice from one of my tutors since I got help with the big 3.

I think for this I will keep smart pointers out, and probably encouraged not to use that right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void binarytree::operator=(const binarytree& source)
{
	if (this != &source)
	{
		clear(root);
		if(source.root != nullptr)
		copy(source.root, root);
	}

}

void binarytree::copy(treenode* dest, treenode*& source)
{
	if (source == nullptr)
	{
		dest = nullptr;
	}
	else
	{
		source->data = dest->data;
		source = new treenode;
		copy(source->left, dest->left);
		copy(source->right, dest->right);
	}
}


My headers are:
1
2
3
4
static void copy(treenode* destroot, treenode*& source);

//public
void operator=(const binarytree& root);


here's my destructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//header
~binarytree();

//cpp
binarytree::~binarytree()
{
	clear(root);
	cout << "Destructor executed" << endl;
}
//main 
	//Testing the destructor
	binarytree test1;


	test1.~binarytree();


Oh shoot I think I see my error, some code has the destructor as a virtual. The other part is i am passing in clear(root). Normally I just call clear() inside the destructor, so ya it keeps deleting itself I think. My other issue is I had "source = new treenode;" after actually copying the data over, can't do that.

Ok so I think because I have a struct of treenode in my private that is inside my binary tree class I apparently have to make the destructor virtual?
Last edited on
do you understand the diff between = and == ?
look closely.
@jonnin I re-edited it, take a look. It's my damn laptop it likes to multi input sometimes.
Last edited on
Sleep tight guys I figured it out. I also had 2 objects in main that were created so it was normal to have multiple destructions.
Topic archived. No new replies allowed.