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.
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.
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.
@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.
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?