From 2d923ea9b7299c6333775dc8d427cd206e631ec9 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Fri, 1 Jul 2022 01:16:07 +1000 Subject: [PATCH] ecs: fix memory corruption in set/removeComponent --- ecs/src/entities.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ecs/src/entities.zig b/ecs/src/entities.zig index c8779786..28c8cf93 100644 --- a/ecs/src/entities.zig +++ b/ecs/src/entities.zig @@ -572,9 +572,11 @@ pub const Entities = struct { // Update the storage/column for the new component. current_archetype_storage.set(entities.allocator, new_row, name, component); - var swapped_entity_id = archetype.get(entities.allocator, old_ptr.row_index, "id", EntityID).?; archetype.remove(old_ptr.row_index); + const swapped_entity_id = archetype.get(entities.allocator, old_ptr.row_index, "id", EntityID).?; // TODO: try is wrong here and below? + // if we removed the last entry from archetype, then swapped_entity_id == entity + // so the second entities.put will clobber this one try entities.entities.put(entities.allocator, swapped_entity_id, old_ptr); try entities.entities.put(entities.allocator, entity, Pointer{ @@ -666,9 +668,11 @@ pub const Entities = struct { } } - var swapped_entity_id = archetype.get(entities.allocator, old_ptr.row_index, "id", EntityID).?; archetype.remove(old_ptr.row_index); + const swapped_entity_id = archetype.get(entities.allocator, old_ptr.row_index, "id", EntityID).?; // TODO: try is wrong here and below? + // if we removed the last entry from archetype, then swapped_entity_id == entity + // so the second entities.put will clobber this one try entities.entities.put(entities.allocator, swapped_entity_id, old_ptr); try entities.entities.put(entities.allocator, entity, Pointer{