alecthomas / entityx

EntityX - A fast, type-safe C++ Entity-Component system
MIT License
2.23k stars 295 forks source link

[Bug?] ~EntityX() is not calling on_destroy event. (compile_time branch) #175

Closed roig closed 7 years ago

roig commented 7 years ago

Hello alecthomas, if you create an entity without any component and set the callbacks for on_create and on_destroy. It calls on_create callback correctly, but when my program exits, the destructor is not calling on_destroy callback.

I debugged, and found that is not entering inside the "if" (see the code), maybe because the entity has no components, i think it should call on_destroy without components attached to the entity, but I'm not sure how to fix it, any ideas ?

  ~EntityX() {
    for (std::size_t i = 0; i < entity_component_mask_.size(); i++)
      if (entity_component_mask_[i].any())
          destroy(Id(i, entity_version_[i]));
  }

Thank you!

iamPHEN commented 7 years ago

Can you check if your hitting issue #28? Symptoms sound similar.

roig commented 7 years ago

Hello, i checked that issue, but no, I think it isn't related. This code is from the "compile_time" branch, the code is completely different.

The conditional is wrong I think, because it is checking every entity that has a component, but what about destroying entities that doesn't have a component? We will have to wait for alec, to check that.

thanks.

alecthomas commented 7 years ago

@roig you are correct that the destructor is not called, though this is somewhat intentional. EntityX maintains a free list of deleted entity slots, so to determine whether an Entity is truly valid that list needs to be walked for each ID. This could be very slow depending on the number of entities in the free list.

That said, it's still a bug.

roig commented 7 years ago

Thank you alec!