From 3234b6c0ddf1550c13f50756897d13b6d0e4214e Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Tue, 7 Jun 2022 21:06:49 +1000 Subject: [PATCH] ecs: fix pointer invalidation in set/removeComponent --- ecs/src/entities.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ecs/src/entities.zig b/ecs/src/entities.zig index 20279580..b0fda11f 100644 --- a/ecs/src/entities.zig +++ b/ecs/src/entities.zig @@ -371,6 +371,10 @@ pub const Entities = struct { // new component was added), or the same archetype storage table (if just updating the // value of a component.) var archetype_entry = try entities.archetypes.getOrPut(entities.allocator, new_hash); + + // WARNING: entities.archetypes.getOrPut() can invalidate archetype, so we refresh it here + archetype = entities.archetypeByID(entity); + if (!archetype_entry.found_existing) { archetype_entry.value_ptr.* = ArchetypeStorage{ .allocator = entities.allocator, @@ -488,6 +492,10 @@ pub const Entities = struct { // new component was added), or the same archetype storage table (if just updating the // value of a component.) var archetype_entry = try entities.archetypes.getOrPut(entities.allocator, new_hash); + + // WARNING: entities.archetypes.getOrPut() can invalidate archetype, so we refresh it here + archetype = entities.archetypeByID(entity); + if (!archetype_entry.found_existing) { archetype_entry.value_ptr.* = ArchetypeStorage{ .allocator = entities.allocator,