Fix segfault when freeing entities.

This commit is contained in:
Steve 2018-02-22 19:03:48 +00:00
parent b62b9fece7
commit dba5248a41
2 changed files with 14 additions and 7 deletions

View File

@ -122,8 +122,12 @@ void doEntities(void)
free(self); free(self);
/* assign prev entity to self */
self = prev; self = prev;
/* assign prev as self, so that prev doesn't point at the now freed memory */
prev = self;
continue; continue;
} }
@ -189,13 +193,14 @@ void doEntities(void)
{ {
self->touch(touched[i]); self->touch(touched[i]);
/* for objects that never move */
if (touched[i]->isStatic) if (touched[i]->isStatic)
{ {
oldSelf = self; oldSelf = self;
self = touched[i]; self = touched[i];
touched[i]->touch(oldSelf); /* for objects that never move */ touched[i]->touch(oldSelf);
self = oldSelf; self = oldSelf;
} }
@ -526,7 +531,7 @@ static int canWalkOnEntity(float x, float y)
SDL_Rect srcRect; SDL_Rect srcRect;
srcRect.x = x; srcRect.x = x;
srcRect.x = y; srcRect.y = y;
srcRect.w = 8; srcRect.w = 8;
srcRect.h = MAP_TILE_SIZE * 4; srcRect.h = MAP_TILE_SIZE * 4;

View File

@ -143,12 +143,14 @@ static void resizeQTEntCapacity(Quadtree *root)
static int getIndex(Quadtree *root, int x, int y, int w, int h) static int getIndex(Quadtree *root, int x, int y, int w, int h)
{ {
int index = -1; int index, verticalMidpoint, horizontalMidpoint, topQuadrant, bottomQuadrant;
int verticalMidpoint = root->x + (root->w / 2); index = -1;
int horizontalMidpoint = root->y + (root->h / 2);
int topQuadrant = (y < horizontalMidpoint && y + h < horizontalMidpoint); verticalMidpoint = root->x + (root->w / 2);
int bottomQuadrant = (y > horizontalMidpoint); horizontalMidpoint = root->y + (root->h / 2);
topQuadrant = (y < horizontalMidpoint && y + h < horizontalMidpoint);
bottomQuadrant = (y > horizontalMidpoint);
if (x < verticalMidpoint && x + w < verticalMidpoint) if (x < verticalMidpoint && x + w < verticalMidpoint)
{ {