Sign in to follow this  
Mastadex

Memory Error - HELP

Recommended Posts

Mastadex    200
My game is crashing at the destructor call of one of my objects. Basically here is what I have briefly: void func() { Obj *ObjPtr[65]; for (int x = 0; x < 65; x++) { ObjPtr[x] = new Obj(params, params); } // most of the function code goes here for (int x = 0; x < 65; x++) { delete ObjPtr[x]; <-----------CRASH } } Honestly, I have tried everything i know to fic this. Even switching it to: delete [] ObjPtr; That dodnt work either. Im using VisualStudio .NET 2003 and the specific error i get is: Debug Error DAMAGE: after normal block 0x########. Any suggestions?

Share this post


Link to post
Share on other sites
chollida1    532
This warning means that your trying to deletememory that doesn't belong to you. Usually its been deleted allready, or it was never allocated and your pointing to memory that you didnt' allocate.

What is the value of x at this time??
try setting the values to NULL after you delete them, you'll find this practice saves you a good deal of time.

Cheers
Chris

Share this post


Link to post
Share on other sites
hplus0603    11356
Stompy's change won't change anything.

I suggest you look into what specific address it's complaining about, and set a data breakpoint in your program after allocation, and see who changes it.

Also, you can cut out parts of the program, until it works, and then add parts back until it breaks; then you know where to look for the problem.

Share this post


Link to post
Share on other sites
Mastadex    200
I tried it both ways, it still complains. Any other suggestions are appreciated. Although the program does work fine without including the delete statement. But i know thats a huge memory leak...

Share this post


Link to post
Share on other sites
Kibble    504
The problem is not the delete, that message probably means that you overwrote the 'fences' around your block of memory. The fences are what the debugger uses to tell if you've written past the ends of your array/memory. If it doesn't find the values it puts at the ends of the block when you delete the memory, it gives you that message because it knows you wrote something outside of the allocated memory.

Check for array indices out of bounds, that sort of thing.

Share this post


Link to post
Share on other sites
iMalc    2466
I would virtually guarantee that you have an array bounds overflow problem. For probably every other person on GDNET who's had that problem, that was the cause. It's also extremely likely that you aren't using asserts, which is also common in cases like this.

The answer is to put assert()'s in at the place where you access an array, particularly whenever you write to an array, assert that the index is ">= 0" and "< arrayMax". Most likely, this WILL show you what the error is. Do this now.

Share this post


Link to post
Share on other sites
moeron    326
Another way to check if you'r deleting stuff that has been deleted before would be to set ObjPtr[x] = 0 after you delete it. This won't help if you've got dangling pointers somewhere else, but it will let you see if your deleting the same thing twice from ObjPtr...

hth
moe.ron

Share this post


Link to post
Share on other sites
chollida1    532
Quote:
Original post by moeron
Another way to check if you'r deleting stuff that has been deleted before would be to set ObjPtr[x] = 0 after you delete it. This won't help if you've got dangling pointers somewhere else, but it will let you see if your deleting the same thing twice from ObjPtr...

hth
moe.ron


ahhhm

What is the value of x at this time??
try setting the values to NULL after you delete them, you'll find this practice saves you a good deal of time.

This has been covered:)

CHeers
Chris

Share this post


Link to post
Share on other sites
Mastadex    200
Setting the Pointer to null doe not give me different results. I still get the error. Is there an alternate way of allocating memory for an array of 64 Obj? Im willing to try anything at this point.

Share this post


Link to post
Share on other sites
I had a problem more or less similar a while ago... might be completely different.
Is your 'Obj' class a derived class?
If it's a derived class and the base class is an abstract class, make sure you have a virtual destructor defined in your base class...

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Mastadex
Setting the Pointer to null doe not give me different results. I still get the error. Is there an alternate way of allocating memory for an array of 64 Obj? Im willing to try anything at this point.
Anything huh? Well how about putting in asserts like I mentioned earlier. I say again, that "It most likely WILL show you exactly where the error is!"
Kibble and I are NOT just guessing here, You are overwriting past the end of an allocated block of memory somehow, that is what the error is telling you. Neither the 'new', nor the 'delete' keyword is where the problem is here.

PLEASE, just try using asserts, and give some feedback as to how that worked out. If you need more information about asserts, then feel free to ask.[smile]

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