Memory Error - HELP

Started by
11 comments, last by iMalc 19 years ago
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?
Advertisement
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
CheersChris
Try changing:

Obj *ObjPtr[65];

to:

Obj *ObjPtr;
ObjPtr = new Obj[65];

that hopefully might help.
-----------------------------Play Stompy's Revenge! Now!
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.
enum Bool { True, False, FileNotFound };
Just thought it might help to delibritely declare the array with new. Guess not, though.
-----------------------------Play Stompy's Revenge! Now!
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...
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.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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
moe.ron
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
CheersChris

This topic is closed to new replies.

Advertisement