Page 1 of 1

Reaching the end of memory!

Posted: 03 Dec 2005, 20:08
by krogothe
It seems that my AI is using up all its memory when im calculating huge map arrays (size of a metal map), giving me a load of errors!
The problem itself doesnt seem to be the size of the arrays but when i use a for loop for each element of the array more than 50+ times i get an error saying cant read memory from 0xfffffffffffff (obviously ran out of memory!)
Each time the loop is run a few elements are changed, but why the hell is it taking up all the memory? (copies of the elements being created maybe?)
its driving me nuts :evil:

Posted: 03 Dec 2005, 20:16
by jcnossen
The chances are that you are doing something wrong, not just gradually allocating all memory. If you were, the application would first slow down and fill the HD with cached virtual memory... you usually don't get NULL from the new operator.

Somewhere you must have misused a pointer and stored -1 (0xffffffff) into it..

Posted: 03 Dec 2005, 20:56
by krogothe
but the error only occurs when i run the loops over X times...
I run 100-1000 loops per pixel on the map, its a shitload of loops, many many millions of them...
is there a way to clear the memory used after each iteration? (besides the array)

heres the code in question (a metal handler):

Code: Select all


	for (int a = 0; a != 20; a++)
	{
		TempMetal = 0;
		for (int i = 0; i != TotalCells; i++)
		{			
			if (MexArrayB[i] > TempMetal)
			{
				TempMetal = MexArrayB[i];
				coordx = i%MetalMapWidth;
				coordy = i/MetalMapWidth;
			}
		}
		
	
		if (TempMetal)
		{
			for (int myx = coordx - XtractorRadius; myx != coordx + XtractorRadius; myx++)
			{
				if (myx >= 0 && myx <= MetalMapWidth)
				{
					for (int myy = coordy - XtractorRadius; myy != coordy + XtractorRadius; myy++)
					{
						if (myy >= 0 && myy <= MetalMapHeight && ((coordx - myx)*(coordx - myx) + (coordy - myy)*(coordy - myy)) <= XtractorRadius*XtractorRadius)
						{
							MexArrayB[myy * MetalMapWidth + myx] = 0;
						}
					}
				}
			}
		}
		MexArrayA[coordy * MetalMapWidth + coordx] = TempMetal;
	}
It seems to me that the program is spawning so many myx and myys that it takes up all the memory...

Posted: 03 Dec 2005, 20:58
by jcnossen
Could you post some code? it will be easier to understand what is happening...

Posted: 03 Dec 2005, 21:06
by krogothe
^^^

Posted: 03 Dec 2005, 21:13
by jcnossen
Ok well you're not allocating any heap memory, so it's impossible to fill up the memory.

You're probably not bounds-checking correctly:

Code: Select all

   if (myx >= 0 && myx <= MetalMapWidth) 
should be
    if (myx >= 0 && myx < MetalMapWidth) 

and 

   myy >= 0 && myy <= MetalMapHeight
should be
   myy >= 0 && myy < MetalMapHeight

Posted: 04 Dec 2005, 01:04
by krogothe
wooooo i fixed it!
it was both what zaph said and bad programming that caused it!
thanks