From 7ef5076cd4a30d7d04efae050da9b49160c59ed5 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Tue, 17 Dec 2024 13:20:39 -0800 Subject: [PATCH] Fix issue with reregistering a component's symbol after deletion --- include/flecs/addons/cpp/component.hpp | 2 +- .../flecs/addons/cpp/mixins/module/impl.hpp | 1 - test/cpp/project.json | 4 +++- test/cpp/src/Entity.cpp | 1 + test/cpp/src/Module.cpp | 18 ++++++++++++++++ test/cpp/src/World.cpp | 21 +++++++++++++++++++ test/cpp/src/main.cpp | 14 +++++++++++-- 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/include/flecs/addons/cpp/component.hpp b/include/flecs/addons/cpp/component.hpp index f4702128f..76b1684e9 100644 --- a/include/flecs/addons/cpp/component.hpp +++ b/include/flecs/addons/cpp/component.hpp @@ -185,7 +185,7 @@ struct type_impl { // component has not yet been registered, or the component is used // across more than one binary), or if the id does not exists in the // world (indicating a multi-world application), register it. - if (!s_id || (world && !ecs_exists(world, s_id))) { + if (!s_id || (world && !ecs_is_alive(world, s_id))) { init(s_id ? s_id : id, allow_tag); ecs_assert(!id || s_id == id, ECS_INTERNAL_ERROR, NULL); diff --git a/include/flecs/addons/cpp/mixins/module/impl.hpp b/include/flecs/addons/cpp/mixins/module/impl.hpp index a76e128c1..d8ee63487 100644 --- a/include/flecs/addons/cpp/mixins/module/impl.hpp +++ b/include/flecs/addons/cpp/mixins/module/impl.hpp @@ -48,7 +48,6 @@ flecs::entity import(world& world) { ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false); if (!_::type::registered(world)) { - /* Module is registered with world, initialize static data */ if (m) { _::type::init(m, false); diff --git a/test/cpp/project.json b/test/cpp/project.json index 40371a9d7..aa7ea4f62 100644 --- a/test/cpp/project.json +++ b/test/cpp/project.json @@ -1104,7 +1104,8 @@ "rename_namespace_longer", "rename_namespace_nested", "rename_reparent_root_module", - "no_recycle_after_rename_reparent" + "no_recycle_after_rename_reparent", + "reimport_after_delete" ] }, { "id": "ImplicitComponents", @@ -1167,6 +1168,7 @@ "reregister_after_reset", "reregister_after_reset_w_namespace", "reregister_namespace", + "reregister_after_delete", "implicit_reregister_after_reset", "reregister_after_reset_different_name", "register_short_template", diff --git a/test/cpp/src/Entity.cpp b/test/cpp/src/Entity.cpp index 979c9c809..6a22a7b06 100644 --- a/test/cpp/src/Entity.cpp +++ b/test/cpp/src/Entity.cpp @@ -4896,6 +4896,7 @@ void Entity_iter_empty_type(void) { int32_t count = 0; for (auto id : e.type()) { + test_assert(id != 0); count ++; } diff --git a/test/cpp/src/Module.cpp b/test/cpp/src/Module.cpp index d33e2b536..e8e6d48f8 100644 --- a/test/cpp/src/Module.cpp +++ b/test/cpp/src/Module.cpp @@ -528,3 +528,21 @@ void Module_no_recycle_after_rename_reparent(void) { test_assert(p == 0); test_str(m.name(), "MyModule"); } + +void Module_reimport_after_delete(void) { + flecs::world ecs; + + { + auto m = ecs.import(); + test_assert(m.lookup("Position") == ecs.component()); + test_assert(m == ecs.entity()); + } + + ecs.entity().destruct(); + + { + auto m = ecs.import(); + test_assert(m.lookup("Position") == ecs.component()); + test_assert(m == ecs.entity()); + } +} diff --git a/test/cpp/src/World.cpp b/test/cpp/src/World.cpp index 838eade12..1dff2c2d1 100644 --- a/test/cpp/src/World.cpp +++ b/test/cpp/src/World.cpp @@ -291,6 +291,27 @@ void World_reregister_after_reset_different_name(void) { ecs.component("Velocity"); } +void World_reregister_after_delete(void) { + flecs::world ecs; + + auto c = ecs.component(); + test_str(c.name(), "Position"); + test_str(c.path(), "::Position"); + test_str(c.symbol(), "Position"); + + c.destruct(); + + test_assert(!c.is_alive()); + + auto d = ecs.component(); + test_assert(c == d); + test_assert(c.is_alive()); + + test_str(c.name(), "Position"); + test_str(c.path(), "::Position"); + test_str(c.symbol(), "Position"); +} + void World_register_component_w_reset_in_multithreaded(void) { flecs::world ecs; diff --git a/test/cpp/src/main.cpp b/test/cpp/src/main.cpp index 7c67a43b7..ff63ac3a2 100644 --- a/test/cpp/src/main.cpp +++ b/test/cpp/src/main.cpp @@ -1066,6 +1066,7 @@ void Module_rename_namespace_longer(void); void Module_rename_namespace_nested(void); void Module_rename_reparent_root_module(void); void Module_no_recycle_after_rename_reparent(void); +void Module_reimport_after_delete(void); // Testsuite 'ImplicitComponents' void ImplicitComponents_add(void); @@ -1122,6 +1123,7 @@ void World_different_comp_same_name(void); void World_reregister_after_reset(void); void World_reregister_after_reset_w_namespace(void); void World_reregister_namespace(void); +void World_reregister_after_delete(void); void World_implicit_reregister_after_reset(void); void World_reregister_after_reset_different_name(void); void World_register_short_template(void); @@ -5558,6 +5560,10 @@ bake_test_case Module_testcases[] = { { "no_recycle_after_rename_reparent", Module_no_recycle_after_rename_reparent + }, + { + "reimport_after_delete", + Module_reimport_after_delete } }; @@ -5768,6 +5774,10 @@ bake_test_case World_testcases[] = { "reregister_namespace", World_reregister_namespace }, + { + "reregister_after_delete", + World_reregister_after_delete + }, { "implicit_reregister_after_reset", World_implicit_reregister_after_reset @@ -7002,7 +7012,7 @@ static bake_test_suite suites[] = { "Module", NULL, NULL, - 22, + 23, Module_testcases }, { @@ -7023,7 +7033,7 @@ static bake_test_suite suites[] = { "World", NULL, NULL, - 116, + 117, World_testcases }, {