Shift dead entities out of main list instead of freeing, to counter dangling pointers.
This commit is contained in:
parent
a541cf1976
commit
1b1a4ca792
|
@ -50,6 +50,8 @@ void initBattle(void)
|
||||||
app.delegate.draw = &draw;
|
app.delegate.draw = &draw;
|
||||||
memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
|
memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
|
||||||
|
|
||||||
|
initEntities();
|
||||||
|
|
||||||
initStars();
|
initStars();
|
||||||
|
|
||||||
initBackground();
|
initBackground();
|
||||||
|
@ -376,6 +378,8 @@ void destroyBattle(void)
|
||||||
|
|
||||||
resetMessageBox();
|
resetMessageBox();
|
||||||
|
|
||||||
|
destroyEntities();
|
||||||
|
|
||||||
destroyScript();
|
destroyScript();
|
||||||
|
|
||||||
destroyGrid();
|
destroyGrid();
|
||||||
|
|
|
@ -45,6 +45,8 @@ extern void blit(SDL_Texture *texture, int x, int y, int centered);
|
||||||
extern void initHud(void);
|
extern void initHud(void);
|
||||||
extern void initRadar(void);
|
extern void initRadar(void);
|
||||||
extern void initGalacticMap(void);
|
extern void initGalacticMap(void);
|
||||||
|
extern void initEntities(void);
|
||||||
|
extern void destroyEntities(void);
|
||||||
extern void drawWidgets(char *groupName);
|
extern void drawWidgets(char *groupName);
|
||||||
extern void selectWidget(const char *name, const char *group);
|
extern void selectWidget(const char *name, const char *group);
|
||||||
extern Widget *getWidget(const char *name, const char *group);
|
extern Widget *getWidget(const char *name, const char *group);
|
||||||
|
|
|
@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
#include "entities.h"
|
#include "entities.h"
|
||||||
|
|
||||||
|
static Entity deadHead;
|
||||||
|
static Entity *deadTail;
|
||||||
|
|
||||||
static void drawEntity(Entity *e);
|
static void drawEntity(Entity *e);
|
||||||
static void doEntity(void);
|
static void doEntity(void);
|
||||||
static void alignComponents(void);
|
static void alignComponents(void);
|
||||||
|
@ -29,6 +32,13 @@ static void restrictToGrid(Entity *e);
|
||||||
static void drawTargetRects(Entity *e);
|
static void drawTargetRects(Entity *e);
|
||||||
static int drawComparator(const void *a, const void *b);
|
static int drawComparator(const void *a, const void *b);
|
||||||
|
|
||||||
|
void initEntities(void)
|
||||||
|
{
|
||||||
|
memset(&deadHead, 0, sizeof(Entity));
|
||||||
|
|
||||||
|
deadTail = &deadHead;
|
||||||
|
}
|
||||||
|
|
||||||
Entity *spawnEntity(void)
|
Entity *spawnEntity(void)
|
||||||
{
|
{
|
||||||
Entity *e = malloc(sizeof(Entity));
|
Entity *e = malloc(sizeof(Entity));
|
||||||
|
@ -56,7 +66,7 @@ void doEntities(void)
|
||||||
|
|
||||||
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
||||||
{
|
{
|
||||||
if (e->active)
|
if (e->active && e->alive != ALIVE_DEAD)
|
||||||
{
|
{
|
||||||
addToGrid(e);
|
addToGrid(e);
|
||||||
}
|
}
|
||||||
|
@ -97,7 +107,7 @@ void doEntities(void)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e->alive == ALIVE_ALIVE || e->alive == ALIVE_DYING)
|
if (e->alive != ALIVE_DEAD)
|
||||||
{
|
{
|
||||||
if (e->action != NULL)
|
if (e->action != NULL)
|
||||||
{
|
{
|
||||||
|
@ -142,7 +152,12 @@ void doEntities(void)
|
||||||
cutRope(e);
|
cutRope(e);
|
||||||
|
|
||||||
prev->next = e->next;
|
prev->next = e->next;
|
||||||
free(e);
|
|
||||||
|
/* move to dead list */
|
||||||
|
e->next = NULL;
|
||||||
|
deadTail->next = e;
|
||||||
|
deadTail = e;
|
||||||
|
|
||||||
e = prev;
|
e = prev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -431,3 +446,17 @@ static int drawComparator(const void *a, const void *b)
|
||||||
|
|
||||||
return e2->type - e1->type;
|
return e2->type - e1->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void destroyEntities(void)
|
||||||
|
{
|
||||||
|
Entity *e;
|
||||||
|
|
||||||
|
while (deadHead.next)
|
||||||
|
{
|
||||||
|
e = deadHead.next;
|
||||||
|
deadHead.next = e->next;
|
||||||
|
free(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
deadTail = &deadHead;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue