Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

refactor(engine): RAII onDestroy subscriptions fix dangling-callback leaks (Particle/Spine)

esengine44425bcc9460
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index 05663afcf..d3a2552c3 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -24,6 +24,7 @@
#include "Entity.hpp"
#include "SparseSet.hpp"
#include "View.hpp"
+#include "../events/Connection.hpp"
// Standard library
#include <algorithm>
@@ -199,6 +200,18 @@ public:
}
}
+ /**
+ * RAII variant of onDestroy: the returned Connection auto-unregisters the
+ * callback when it is destroyed, so a subscriber that holds it as a member
+ * can never leak a dangling `this` into the registry. Prefer this over the
+ * raw onDestroy()/removeOnDestroy() pair.
+ */
+ [[nodiscard]] Connection onDestroyScoped(DestroyCallback callback) {
+ u32 id = onDestroy(std::move(callback));
+ return Connection(static_cast<CallbackId>(id),
+ [this](CallbackId cid) { removeOnDestroy(static_cast<u32>(cid)); });
+ }
+
/**
* @brief Checks if an entity is currently valid
* @param entity The entity to check
@@ -608,7 +621,9 @@ private:
};
std::vector<DestroyEntry> on_destroy_entries_;
- u32 next_callback_id_ = 0;
+ // Starts at 1: id 0 is reserved as "no connection" (Connection's
+ // INVALID_CALLBACK_ID) and as the not-yet-registered sentinel.
+ u32 next_callback_id_ = 1;
u32 firing_destroy_ = 0;
};
diff --git a/src/esengine/particle/ParticleSystem.cpp b/src/esengine/particle/ParticleSystem.cpp
index bf6fda1fb..4899b7278 100644
--- a/src/esengine/particle/ParticleSystem.cpp
+++ b/src/esengine/particle/ParticleSystem.cpp
@@ -10,8 +10,8 @@ ParticleSystem::ParticleSystem()
}
void ParticleSystem::update(ecs::Registry& registry, f32 dt) {
- if (destroy_callback_id_ == 0) {
- destroy_callback_id_ = registry.onDestroy([this](Entity entity) {
+ if (!destroyConn_.isConnected()) {
+ destroyConn_ = registry.onDestroyScoped([this](Entity entity) {
states_.erase(entity);
});
}
diff --git a/src/esengine/particle/ParticleSystem.hpp b/src/esengine/particle/ParticleSystem.hpp
index 60d9e0fa6..aeea2c6ee 100644
--- a/src/esengine/particle/ParticleSystem.hpp
+++ b/src/esengine/particle/ParticleSystem.hpp
@@ -56,7 +56,9 @@ private:
std::unordered_map<Entity, EmitterState> states_;
std::mt19937 rng_;
std::vector<u32> dead_particle_indices_;
- u32 destroy_callback_id_ = 0;
+ // RAII: auto-unregisters from the registry's onDestroy when this system is
+ // destroyed, so a torn-down system never leaves a dangling `this` behind.
+ Connection destroyConn_;
};
} // namespace esengine::particle
diff --git a/src/esengine/spine/SpineSystem.cpp b/src/esengine/spine/SpineSystem.cpp
index 0e1bddb93..72bfc620f 100644
--- a/src/esengine/spine/SpineSystem.cpp
+++ b/src/esengine/spine/SpineSystem.cpp
@@ -43,8 +43,8 @@ void SpineSystem::update(ecs::Registry& registry, f32 deltaTime) {
}
}
- if (destroy_callback_id_ == 0) {
- destroy_callback_id_ = registry.onDestroy([this](Entity entity) {
+ if (!destroyConn_.isConnected()) {
+ destroyConn_ = registry.onDestroyScoped([this](Entity entity) {
instances_.erase(entity);
});
}
diff --git a/src/esengine/spine/SpineSystem.hpp b/src/esengine/spine/SpineSystem.hpp
index 96b035baa..8acc09c79 100644
--- a/src/esengine/spine/SpineSystem.hpp
+++ b/src/esengine/spine/SpineSystem.hpp
@@ -217,7 +217,9 @@ private:
SpineResourceManager& resource_manager_;
std::unordered_map<Entity, SpineInstance> instances_;
- u32 destroy_callback_id_ = 0;
+ // RAII: auto-unregisters from the registry's onDestroy on teardown, so a
+ // destroyed system never leaves a dangling `this` in the registry.
+ Connection destroyConn_;
std::array<f32, MAX_NATIVE_EVENTS * EVENT_STRIDE> native_event_buffer_{};
std::array<NativeEventRecord, MAX_NATIVE_EVENTS> native_event_records_{};
diff --git a/tests/ecs/test_registry_safety.cpp b/tests/ecs/test_registry_safety.cpp
index 9dbe49f8f..628948378 100644
--- a/tests/ecs/test_registry_safety.cpp
+++ b/tests/ecs/test_registry_safety.cpp
@@ -91,6 +91,27 @@ int main() {
CHECK(r.valid(a) && r.valid(b), "both recreated entities are valid");
}
+ // --- onDestroyScoped: the RAII Connection auto-unregisters on scope exit ---
+ // A system that stores the raw callback id but forgets to removeOnDestroy
+ // leaves a dangling `this` in the registry (the ParticleSystem / SpineSystem
+ // bug). The scoped variant makes that impossible: the callback is gone once
+ // the returned Connection is destroyed. ASAN here would catch a disconnect
+ // that reached into freed memory.
+ {
+ Registry r;
+ int hits = 0;
+ {
+ auto conn = r.onDestroyScoped([&](Entity) { ++hits; });
+ Entity e = r.create();
+ r.destroy(e);
+ CHECK(hits == 1, "scoped onDestroy fires while the Connection is alive");
+ }
+ // Connection destroyed -> callback unregistered.
+ Entity e2 = r.create();
+ r.destroy(e2);
+ CHECK(hits == 1, "callback no longer fires once the Connection is destroyed");
+ }
+
// --- ES_VERIFY: emplace / emplaceOrReplace on an invalid entity is release-safe ---
// Without the guard, component_masks_[Entity{}.index() == 0xFFFFF] is an OOB
// write (ES_ASSERT is stripped here). The guard must fire and return a fallback.