Fix memory leak with object pools

This commit is contained in:
2024-02-08 21:16:24 +01:00
parent 593c947f2e
commit 8056fda650

View File

@@ -48,12 +48,13 @@ ECS_DTOR(SpatialGridID, gridID, {
}) })
ECS_MOVE(SpatialGridID, dst, src, { ECS_MOVE(SpatialGridID, dst, src, {
*dst = *src; *dst = *src;
*src = 0;
}) })
ECS_DTOR(Path, path, { ECS_DTOR(Path, path, {
Game *game = ecs_singleton_get_mut(ECS, Game); Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData; BzObjectPool *pool = game->pools.pathData;
PathData *cur = path[i].paths; PathData *cur = path->paths;
while (cur) { while (cur) {
bzObjectPoolRelease(pool, cur); bzObjectPoolRelease(pool, cur);
cur = cur->next; cur = cur->next;
@@ -61,7 +62,22 @@ ECS_DTOR(Path, path, {
}) })
ECS_MOVE(Path, dst, src, { ECS_MOVE(Path, dst, src, {
*dst = *src; *dst = *src;
src->paths = NULL;
src->curWaypoint = 0;
}) })
void pathRemoved(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData;
Path *path = ecs_field(it, Path, 1);
for (int i = 0; i < it->count; i++) {
PathData *cur = path[i].paths;
while (cur) {
bzObjectPoolRelease(pool, cur);
cur = cur->next;
}
path[i].paths = NULL;
}
}
ECS_DTOR(Building, building, { ECS_DTOR(Building, building, {
Vec2i pos = building->pos; Vec2i pos = building->pos;
@@ -71,6 +87,7 @@ ECS_DTOR(Building, building, {
}) })
ECS_MOVE(Building, dst, src, { ECS_MOVE(Building, dst, src, {
*dst = *src; *dst = *src;
*src = (Building) {.type = 0};
}) })
void delayDeleteUpdate(ecs_iter_t *it) { void delayDeleteUpdate(ecs_iter_t *it) {
@@ -93,7 +110,8 @@ void setupSystems() {
}); });
ecs_set_hooks(ECS, Path, { ecs_set_hooks(ECS, Path, {
.dtor = ecs_dtor(Path), .dtor = ecs_dtor(Path),
.move_dtor = ecs_move(Path) .move = ecs_move(Path),
.on_remove = pathRemoved
}); });
ecs_set_hooks(ECS, Building, { ecs_set_hooks(ECS, Building, {
.dtor = ecs_dtor(Building), .dtor = ecs_dtor(Building),