Jump to content
  • Advertisement
Sign in to follow this  

Odd delete error

This topic is 4740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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).

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!