Fix memory leak when iterating path

This commit is contained in:
2023-11-23 19:35:59 +01:00
parent a17450dd81
commit 6e977b7433
6 changed files with 17 additions and 2 deletions

View File

@@ -42,6 +42,16 @@ void bzObjectPoolDestroy(BzObjectPool *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) {
if (pool->firstFree == -1)
return NULL;

View File

@@ -13,6 +13,7 @@ typedef struct BzObjectPoolDesc {
BzObjectPool *bzObjectPoolCreate(const BzObjectPoolDesc *desc);
void bzObjectPoolDestroy(BzObjectPool *pool);
size_t bzObjectPoolCalcNumFree(BzObjectPool *pool);
void *bzObjectPool(BzObjectPool *pool);
void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx);
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object);

View File

@@ -191,7 +191,7 @@ void update(float dt, void *userData) {
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
SetWindowTitle(titleBuf);
updatePlayerInput(NULL);
updatePlayerInput(NULL);
}
void render(float dt, void *userData) {
@@ -224,6 +224,7 @@ void imguiRender(float dt, void *userData) {
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
igBegin("Debug Menu", NULL, 0);
igText("Num paths from pool available: %llu", bzObjectPoolCalcNumFree(game->pools.pathData));
const char *inputState = "NONE";
switch (input->state) {
case INPUT_NONE:

View File

@@ -84,7 +84,6 @@ static void smoothPath(BzTileMap *map, PathData *pathData, BzObjectPool *pool) {
prevIdx++;
nextIdx++;
bzLogInfo("%llu,%llu", prevIdx, nextIdx);
if (prevIdx >= currPathLen) {
prevPath = prevPath->next;

View File

@@ -43,6 +43,7 @@ void entityUpdateSpatialID(ecs_iter_t *it);
void entityUpdateKinematic(ecs_iter_t *it);
/*
* 0: Game (singleton) for object pool
* 1: Position
* 2: Rotation
* 3: Velocity

View File

@@ -78,6 +78,7 @@ void entityUpdateKinematic(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);
Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
@@ -95,6 +96,8 @@ void entityFollowPath(ecs_iter_t *it) {
path[i].curWaypoint++;
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
path[i].curWaypoint = 0;
PathData *oldPath = path[i].paths;
bzObjectPoolRelease(game->pools.pathData, oldPath);
path[i].paths = path[i].paths->next;
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
}