SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.46k stars 454 forks source link

crash before invoking OnRemove observer of sparse component #1360

Closed jpeletier closed 1 month ago

jpeletier commented 1 month ago

Describe the bug When destroying an entity that has a sparse component and there is an observer tracking the sparse component, I get the following assert:

fatal: sparse.c: 621: assert: dense < sparse->count INTERNAL_ERROR

Probably related to #1358 since the crash is in the same line.

To Reproduce

flecs::world ecs;
struct Attribute {
  int value;
};

auto attribute = ecs.component<Attribute>().add(flecs::Sparse);
ecs.observer<Attribute>()
  .event(flecs::OnRemove)
  .each([](flecs::entity e, Attribute &attr) {
  std::cout << "Attibute removed" << std::endl;
});

auto e = ecs.entity().add<Attribute>();

e.destruct();  // fatal: sparse.c: 621: assert: dense < sparse->count INTERNAL_ERROR

Expected behavior observer is called normally, as if the component wasn't sparse.

SanderMertens commented 1 month ago

Fixed!