Wierd heap overloading problem I can't understand or sort-out

Started by
20 comments, last by persil 18 years, 11 months ago
Hello guys. I'm doing this king of programming thingy, which generates an error I cannot understand. Here are the details:
GameCore::~GameCore() // ~Destructor :]
{
	delete engine;		

	for(int j=Map_Size_X - 1;j>=0;j--)
		for(int i=Map_Size_Y - 1;i>=0;i--)
		{						
			delete Map[j];

			if(j<Visible_Map_Size_X && i<Visible_Map_Size_Y)
				delete VisibleMap[j];
		}

	SDL_Quit();
}

The thing is when I'm running on debug mode (not release mode) on my Visual C++ 6, I'm getting a debug error telling there's some overloading on the heap, or something like that. If I try to take out that whole deletion loop (leaving the function with only deleting the engine variable and running SDL_Quit() ), I'm getting another error telling me that there is a DAMAGE (Yes, with capital letters) and a memory block adress, or something like that. O_o VisibleMap is 40x30 in size and Map is 100x100. Here are the details of the type of Map and VisibleMap:
class MapTile // This is the info class for every tile on the map.
{
public:
	
	int x,y; // Place in the map array.
	KrTile *Tile; // This is the image of the tile.
	U32 Terrain; // This is the U32 code name of the tile, for easier identification.

	
	MapTile()
	{
		x=0;
		y=0;
		Tile=NULL;
		Terrain = 0;
	}

	~MapTile() 
	{
		Tile=NULL; /* I tried to put 'delete Tile' instead of the
                              current line, but that just gave me another
                              runtime error, probably due to the reason I'm
                              using SDL and the Kyra sprite engine (based on
                              SDL). But that's just cause I think I'm not
                              supposed to delete the Tile images manually,
                              they are already inside some tree which holds
                              their adresses and handles them alone without
                              my intervention. So this current line is
                              supposed to be just fine.*/
	}
};

KrTile, as written above, is a variable that holds an image. The map tile image for this case. Please help, I'm clueless about this one. I'm not experienced enough to know how the main memory heap works, so I can't understand what the hell is causing the trouble. Thanks a lot for yours, anyway. Twiggy :) [Edited by - Twiggy on May 22, 2005 5:55:43 AM]
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
Advertisement
Could you please adjust your post so that we don't have to scroll way across to read it? beware of long lines within [code] [/code] or [source] [/source] tags.

DAMAGE almost always means that you are overwriting the bounds of an array. Typically this is caused by an off-by-one error. Adding asserts around you array accesses will help check for that.
Also beware accessing memory after it has been freed. Sometimes it helps to delete things in the reverse order to what they were created in.
Do not be mistaken, the same error exists in your relase build, but the compiler has not inserted code into your exe to check for it, so it may instead crash later for aparently no reason.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
VisibleMap and Map are both 2d arrays, made using vectors. I don't think vectors (which are practically just dynamic-arrays) can be overwritten, especially for small amounts of information like I use.

Here is the decleration of the variables:
vector<vector<MapTile *> > Map;vector<vector<MapTile *> > VisibleMap;
Btw, if you look on the first post, you'll see that I release the vectors in a reverse order, from the end to the beginning.

Anybody else has some kind of idea what's causing this?

Oh yeah, and I edited to post to look more comfortable. Sorry for that. :)
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
The vector operator[] can access data outside the memory allocated by the vector.

vector::operator[] will return a reference, even if the reference is outside the vector's bounds.

vector::at works like [] but checks the array bounds and throws an exception if you try to access an invalid argument.
iMalc is right - this means you've overwritten something you shouldn't have. It only shows up when you delete the stuff because that's when it checks to see if any of its reserved areas got 'damaged'. So the problem will be somewhere else, probably in how you're adding tiles to the gamecore, or changing them.
But guys, I'm only accessing valid blocks - only the ones inside the vectors. I know this works, because the program works, and I'm getting the error while exiting it - so the use of the Maps variables is proper, because I can see the map on screen on scroll it.

And still, just for checking, i'm having 100x100 + 30x40 tiles saved on the tree, for quick use, when each tile is 16x16 pixels wide, 16 million colors format.

Is this what's causing the problem? To much images loaded on real time?

This seems a bit fishy that my 512 MB pc can't handle this.
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
Just because the output looks right doesn't mean you're making no mistakes. Here's a trivial example:

int* array = new int[2];
array[0] = 1;
array[1] = 1;
array[2] = 246;
cout << array[0] + array[1] << endl;
delete [] array;

That program will probably give you the expected results, but crash when it deletes the array at the end.

There's a small chance that there's an error in one of the libraries you're using, but the error is definitely an unauthorised overwrite of some sort. Consider adding _CrtCheckMemory calls throughout your code along with the debugger to track down where it happens.
As posted in another thread, I would highly reccomend this memory manager.

Just add it to your project and compile. Once the program has finished, it will create a report file indicating if there were or weren't any leaks and tell you what allocated the memory. It will also let you know if any memory was corrupted.
Quote:Original post by Twiggy
But guys, I'm only accessing valid blocks - only the ones inside the vectors. I know this works, because the program works, and I'm getting the error while exiting it - so the use of the Maps variables is proper, because I can see the map on screen on scroll it.
Sure your deleting above might be okay, but your array accesses might be slightly off. You could have written [j] somewhere where you meant [j] for example.
Quote:
And still, just for checking, i'm having 100x100 + 30x40 tiles saved on the tree, for quick use, when each tile is 16x16 pixels wide, 16 million colors format.

Is this what's causing the problem? To much images loaded on real time?
No that is not the cause of the problem.
Quote:
This seems a bit fishy that my 512 MB pc can't handle this.
With experience comes the knowledge that even the bizarrest of problems are usually tracked down to a bug in your own code.

I would also write your delete loop as:
	int i, j;	for(j=Map_Size_X - 1;j>=0;j--)		for(i=Map_Size_Y - 1;i>=0;i--)			delete Map[j];	for(j=Visible_Map_Size_X - 1;j>=0;j--)		for(i=Visible_Map_Size_Y - 1;i>=0;i--)			delete VisibleMap[j];
Mainly because it's slightly more obvious in behaviour, and you're not trying to do more things at once than you need to.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I tried that, that didn't solve the problem, but thanx anyways.

And about switching the [j] & position with eachother - no, it's ok how it is written, and I have proof for it. I'm referring to the maps like that all over the program, and i inerst and take out lots of info, including map pictures, and i see them correctly on the screen. so that's not the problem.

And about the memory manager - thanks, but i seem to miss a certain "stdafx.h" library that the code needs, and I can't find it on the internet, so i'm helpless again.

I still can't seem to know what causes the problem. =
Perhaps if i'll show you how I constructed the whole thing it might help you understand some stuff so you could enlighten me. Damnit, I have to hand in this projectin the morning, and I'm stuck on the latest thing. If I get this done, I'm through.

for(j=0; j<Map_Size_X; j++ ) // j++ here, the script in GameDev deletes                                      // it for some reason.{	Map[j].resize(Map_Size_Y);				if(j<Visible_Map_Size_X)		VisibleMap[j].resize(Visible_Map_Size_Y);}		for(j=0; j < Map_Size_X ; j++ ) // same here			for(i=0; i < Map_Size_Y; i++ ) // and here			{				Map[j] = new MapTile;				if(i<Visible_Map_Size_Y && j<Visible_Map_Size_X)					VisibleMap[j] = new MapTile;			}
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"

This topic is closed to new replies.

Advertisement