[SDL] ot - memory leaks
Patricia Curtis
patricia.curtis at gmail.com
Sun Aug 5 18:35:35 PDT 2007
My approach to memory for any game is i don't create and destroy
memory, the only place i use a malloc is to allocate strings with
pointers for my help and other text, file.
char * StringTable[MAX_STRINGS] ;
read a line
StringTable[ThisString] = (char*)malloc(StringLength);
StringCopy(ReadLine,StringTable[ThisString] ) ;
loop untill all strings are read.........
Then at the end of the game i simply
for(int s=0;s<MAX_STRINGS;s++)
{
if(StringTable[s] )
{
free(StringTable[s]);
}
}
For game objects and what have you, i define an Array of max objects
#typedef struct
{
bool Active;
int NextObject;
int PrevObject;
.... other stuff
}Shape;
Shape GameShapes[MAXBALLS];
Then when i want to create a new object (Shape) i simply find the
first empty Object in my Array by checking the Active flag with a
simple FOR NEXT LOOP which returns the number of the object in the
array.
Then link it using the Next and Prev ints,
insertBeginning(int newObject);
see http://en.wikipedia.org/wiki/Linked_list for info on linked lists.
Then in my loops for drawing and moving the objects i just use
int Ball = FirstLink;
while(Ball!=LAST_LINK)
{
// some code
Ball = GameShapes[Ball].NextLink;
}
Now yes its not pointers and yes its not OO code but it guarantees
that my games don't have memory leaks because i know how much memory
it needs and is using.
it is also faster as there is no garbage collection and each object is
reused over and over.
I have used this method in almost every one my 29 games and it works.
At the end of the day most of us are writing games, and we are not
working for NASA, we want speed not technically perfect code.
Trish x
More information about the SDL
mailing list