I have an obvious question, yet I'm perplexed by the problem.
Ok, first let me overview the situation. I have a structure called ENTITY that is used to hold the attributes for entities in a game. I recently added more members to the structure. The program runs perfect, but when I quit, windows pops up an error screen saying "XXX.exe has stopped working...check online for solution...blah blah".
So, to troubleshoot, I removed a few members from the ENTITY structure and the program runs fine and exits fine. ????
(compiled with Dev-cpp)
The code:
typedef struct _ENTITY
{
char classname[16];
int health;
int vel_x;
int vel_y;
int direction;
int frame;
int flag;
SDL_Rect bbox;
struct _ENTITY *next;
struct _ENTITY *owner;
struct _ENTITY *goal;
void (*think) ();
float nextthink;
} ENTITY;
The function that allocates memory to ENTITY structures
ENTITY *ENTITY_spawn (void)
{
ENTITY *node, *old_node;
int i;
node = ENTITY_head; // Top of list
// Find end of list
for (i = 0; node; i++)
{
old_node = node;
node = node->next;
}
// Allocate
node = (ENTITY*)calloc (1, sizeof (ENTITY));
if (i)
old_node->next = node;
else
ENTITY_head = node;
return node;
}
(EDIT 4/8/12) -Used calloc instead of malloc -Inserted void in function parameters -Got rid of NULL_void -Could not get rid of (ENTITY*) cast, the compiler complains that it could not convert type void (because I didn't include stdlib.h?)
Here's how I remove ENTITY(s) when exiting the program:
void ENTITY_cleanup (void)
{
ENTITY *node, *old_node;
node = ENTITY_head;
while (node)
{
old_node = node->next;
free (node);
node = old_node;
}
}