Shift dead entities out of main list instead of freeing, to counter dangling pointers.

This commit is contained in:
Steve 2015-12-10 10:15:27 +00:00
parent a541cf1976
commit 1b1a4ca792
3 changed files with 38 additions and 3 deletions

View File

@ -50,6 +50,8 @@ void initBattle(void)
app.delegate.draw = &draw;
memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
initEntities();
initStars();
initBackground();
@ -376,6 +378,8 @@ void destroyBattle(void)
resetMessageBox();
destroyEntities();
destroyScript();
destroyGrid();

View File

@ -45,6 +45,8 @@ extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern void initHud(void);
extern void initRadar(void);
extern void initGalacticMap(void);
extern void initEntities(void);
extern void destroyEntities(void);
extern void drawWidgets(char *groupName);
extern void selectWidget(const char *name, const char *group);
extern Widget *getWidget(const char *name, const char *group);

View File

@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "entities.h"
static Entity deadHead;
static Entity *deadTail;
static void drawEntity(Entity *e);
static void doEntity(void);
static void alignComponents(void);
@ -29,6 +32,13 @@ static void restrictToGrid(Entity *e);
static void drawTargetRects(Entity *e);
static int drawComparator(const void *a, const void *b);
void initEntities(void)
{
memset(&deadHead, 0, sizeof(Entity));
deadTail = &deadHead;
}
Entity *spawnEntity(void)
{
Entity *e = malloc(sizeof(Entity));
@ -56,7 +66,7 @@ void doEntities(void)
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{
if (e->active)
if (e->active && e->alive != ALIVE_DEAD)
{
addToGrid(e);
}
@ -97,7 +107,7 @@ void doEntities(void)
break;
}
if (e->alive == ALIVE_ALIVE || e->alive == ALIVE_DYING)
if (e->alive != ALIVE_DEAD)
{
if (e->action != NULL)
{
@ -142,7 +152,12 @@ void doEntities(void)
cutRope(e);
prev->next = e->next;
free(e);
/* move to dead list */
e->next = NULL;
deadTail->next = e;
deadTail = e;
e = prev;
}
}
@ -431,3 +446,17 @@ static int drawComparator(const void *a, const void *b)
return e2->type - e1->type;
}
void destroyEntities(void)
{
Entity *e;
while (deadHead.next)
{
e = deadHead.next;
deadHead.next = e->next;
free(e);
}
deadTail = &deadHead;
}