Fix memory leak when iterating path
This commit is contained in:
@@ -42,6 +42,16 @@ void bzObjectPoolDestroy(BzObjectPool *pool) {
|
|||||||
bzFree(pool);
|
bzFree(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t bzObjectPoolCalcNumFree(BzObjectPool *pool) {
|
||||||
|
size_t count = 0;
|
||||||
|
i32 idx = pool->firstFree;
|
||||||
|
while (idx != -1) {
|
||||||
|
count++;
|
||||||
|
i32 *object = bzObjectPoolGetObject(pool, idx);
|
||||||
|
idx = *object;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
void *bzObjectPool(BzObjectPool *pool) {
|
void *bzObjectPool(BzObjectPool *pool) {
|
||||||
if (pool->firstFree == -1)
|
if (pool->firstFree == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ typedef struct BzObjectPoolDesc {
|
|||||||
BzObjectPool *bzObjectPoolCreate(const BzObjectPoolDesc *desc);
|
BzObjectPool *bzObjectPoolCreate(const BzObjectPoolDesc *desc);
|
||||||
void bzObjectPoolDestroy(BzObjectPool *pool);
|
void bzObjectPoolDestroy(BzObjectPool *pool);
|
||||||
|
|
||||||
|
size_t bzObjectPoolCalcNumFree(BzObjectPool *pool);
|
||||||
void *bzObjectPool(BzObjectPool *pool);
|
void *bzObjectPool(BzObjectPool *pool);
|
||||||
void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx);
|
void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx);
|
||||||
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object);
|
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object);
|
||||||
|
|||||||
@@ -224,6 +224,7 @@ void imguiRender(float dt, void *userData) {
|
|||||||
|
|
||||||
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
||||||
igBegin("Debug Menu", NULL, 0);
|
igBegin("Debug Menu", NULL, 0);
|
||||||
|
igText("Num paths from pool available: %llu", bzObjectPoolCalcNumFree(game->pools.pathData));
|
||||||
const char *inputState = "NONE";
|
const char *inputState = "NONE";
|
||||||
switch (input->state) {
|
switch (input->state) {
|
||||||
case INPUT_NONE:
|
case INPUT_NONE:
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ static void smoothPath(BzTileMap *map, PathData *pathData, BzObjectPool *pool) {
|
|||||||
|
|
||||||
prevIdx++;
|
prevIdx++;
|
||||||
nextIdx++;
|
nextIdx++;
|
||||||
bzLogInfo("%llu,%llu", prevIdx, nextIdx);
|
|
||||||
|
|
||||||
if (prevIdx >= currPathLen) {
|
if (prevIdx >= currPathLen) {
|
||||||
prevPath = prevPath->next;
|
prevPath = prevPath->next;
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ void entityUpdateSpatialID(ecs_iter_t *it);
|
|||||||
void entityUpdateKinematic(ecs_iter_t *it);
|
void entityUpdateKinematic(ecs_iter_t *it);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* 0: Game (singleton) for object pool
|
||||||
* 1: Position
|
* 1: Position
|
||||||
* 2: Rotation
|
* 2: Rotation
|
||||||
* 3: Velocity
|
* 3: Velocity
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void entityFollowPath(ecs_iter_t *it) {
|
void entityFollowPath(ecs_iter_t *it) {
|
||||||
|
const Game *game = ecs_singleton_get(ECS, Game);
|
||||||
Position *position = ecs_field(it, Position, 1);
|
Position *position = ecs_field(it, Position, 1);
|
||||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||||
@@ -95,6 +96,8 @@ void entityFollowPath(ecs_iter_t *it) {
|
|||||||
path[i].curWaypoint++;
|
path[i].curWaypoint++;
|
||||||
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||||
path[i].curWaypoint = 0;
|
path[i].curWaypoint = 0;
|
||||||
|
PathData *oldPath = path[i].paths;
|
||||||
|
bzObjectPoolRelease(game->pools.pathData, oldPath);
|
||||||
path[i].paths = path[i].paths->next;
|
path[i].paths = path[i].paths->next;
|
||||||
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user