From dba5248a415d4ff57967b69207299310ca565c52 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 22 Feb 2018 19:03:48 +0000 Subject: [PATCH] Fix segfault when freeing entities. --- src/world/entities.c | 9 +++++++-- src/world/quadtree.c | 12 +++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/world/entities.c b/src/world/entities.c index 3279ad7..8c7f959 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -122,8 +122,12 @@ void doEntities(void) free(self); + /* assign prev entity to self */ self = prev; + /* assign prev as self, so that prev doesn't point at the now freed memory */ + prev = self; + continue; } @@ -189,13 +193,14 @@ void doEntities(void) { self->touch(touched[i]); + /* for objects that never move */ if (touched[i]->isStatic) { oldSelf = self; self = touched[i]; - touched[i]->touch(oldSelf); /* for objects that never move */ + touched[i]->touch(oldSelf); self = oldSelf; } @@ -526,7 +531,7 @@ static int canWalkOnEntity(float x, float y) SDL_Rect srcRect; srcRect.x = x; - srcRect.x = y; + srcRect.y = y; srcRect.w = 8; srcRect.h = MAP_TILE_SIZE * 4; diff --git a/src/world/quadtree.c b/src/world/quadtree.c index fd11986..6c415a5 100644 --- a/src/world/quadtree.c +++ b/src/world/quadtree.c @@ -143,12 +143,14 @@ static void resizeQTEntCapacity(Quadtree *root) static int getIndex(Quadtree *root, int x, int y, int w, int h) { - int index = -1; + int index, verticalMidpoint, horizontalMidpoint, topQuadrant, bottomQuadrant; + + index = -1; - int verticalMidpoint = root->x + (root->w / 2); - int horizontalMidpoint = root->y + (root->h / 2); - int topQuadrant = (y < horizontalMidpoint && y + h < horizontalMidpoint); - int bottomQuadrant = (y > horizontalMidpoint); + verticalMidpoint = root->x + (root->w / 2); + horizontalMidpoint = root->y + (root->h / 2); + topQuadrant = (y < horizontalMidpoint && y + h < horizontalMidpoint); + bottomQuadrant = (y > horizontalMidpoint); if (x < verticalMidpoint && x + w < verticalMidpoint) {