Sign in to follow this  
NSProgramer

Odd delete error

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this