From 7e338b7e29c936f4eefeb6936c9fc57579571109 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 4 Jul 2023 16:37:09 +0900 Subject: [PATCH] [ecs] Delay calculation of last index Removing a hierarchy from an entity can result in a large number of component removes in the same pool, thus changing index of the lest element of the pool. This *seems* to fix the memory corruption I've been experiencing with the debug UI. --- libs/ecs/entity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/ecs/entity.c b/libs/ecs/entity.c index 1fd315538..2d579cc04 100644 --- a/libs/ecs/entity.c +++ b/libs/ecs/entity.c @@ -116,7 +116,6 @@ Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry) uint32_t ind = pool->sparse[id]; component_t *c = ®istry->components.a[comp]; if (ind < pool->count && pool->dense[ind] == ent) { - uint32_t last = pool->count - 1; // invalidate the entity for this component to prevent the component // being double-removed due to deletion of the component resulting // in the entity being deleted (happens with hierarchies) @@ -137,6 +136,7 @@ Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry) } } } + uint32_t last = pool->count - 1; if (last > ind) { pool->sparse[Ent_Index (pool->dense[last])] = ind; pool->dense[ind] = pool->dense[last];