Odd delete error

Started by
4 comments, last by Telastyn 18 years, 10 months ago
I'm getting a very odd delete error when i try to delete my double pointer (ptr**). Declaration of the ptr** ptr** object = new ptr*[x]; for(y = 0; y < x; y++) { object[y] = new ptr[z]; } Deleting the ptr** for(y = 0; y < x; y++) { delete[] object[y]; } delete[] object; This works all fine and dandy till i get to y = 22 in the deletion loop. Once it hits 22 the program terminates, but if i tell it to skip 22 and go on up to 158 it works just fine. If anyone can tell me whats up with this or a way to test deletion before it forces the program to exit (try/catch don't work....) I would really appreciate it.
Advertisement
The problem is not in this part of the code. It is valid and will not crash by itself. You've got memory corruption elsewhere in your program. (Either that or ptr::~ptr() is throwing an exception -- big nono).
Kippesoep
ptr is actually a struct in this case and has no destructor... probably just going to switch back to c-style memory deallocation/allocation and see if that works, always liked c-style better anyway
Even a struct has a destructor. If you haven't specified it, it's implicit (i.e. generated by the compiler -- this won't throw an exception, though).

Switching to C-style memory management probably won't help you in this case. At most, it'll hide the problem. As I said above, the problem is not here, but in another part of your code that's overwriting the memory here.
Kippesoep
Yes it does. The struct has a DEFAULT CONSTRUCTOR, generated by the compiler. As kippesoep said, the corruption is caused somewhere else.

The error is raised when you're deleting this particular pointer, because the corruption is at that address. There is a very big chance you're overwriting some area of memory. And if you change it to malloc/free, you will probably still get the error.

Double check your memory writings everywhere.

Toolmaker

And using Standard Library containers will save you most all of these headaches in the future. C++ added them so this sort of thing wasn't necissary. Post the entirety of the code [as is] and we might be able to spot something obvious.

This topic is closed to new replies.

Advertisement