alecthomas / entityx

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

~EntityManager() depends on having a valid EventManager #127

Closed iamPHEN closed 8 years ago

iamPHEN commented 8 years ago

I ran into a slight usage issue issue with the following App:

struct App {
   App() : em(ev) {}

    EventManager ev;
    EntityManager em;
 }

 int main() {
    App a;
    a.em.create();
    return 0;
 }

What happens here is:

~App() is called ~EventManager() gets called, all signal handlers get destructed. ~EntityManager() calls EntityManager::reset(), which emits EntityDestroyedEvent

This caused a SEGFAULT in a weird spot during EntityManager::emit<entityx::EntityDestroyedEvent, entityx::Entity>(entityx::Entity&&).

backtrace of crash

Possible solution (not sure whats best) :

iamPHEN commented 8 years ago

As another note: The app is valid when complied on windows with MSVC15 and doesn't segfault on exit.

I'm curious as to why that is, but that's a bit off topic :).

alecthomas commented 8 years ago

According to this post, members should be constructed in the order they appear, and destructed in reverse order. If that is not the case, it seems that this is a compiler bug.

iamPHEN commented 8 years ago

Ah. I was trying to dig for that. Awesome. I'm fine with closing this issue then since the standard recognizes it that way.

Sounds like a MSVC bug then, or me just being lucky that the Signal was still valid after deconstruction of the event manager.

Closing issue.