Closed Ybalrid closed 7 years ago
AnnEngine keeps it's own address into a static "singleton" member, and that is used everywhere. All "AnnGetSomethign" will actually dereference this singleton.
A lot of subsystem owned objects needs to talk to other subsystems (that are still alive) when deleting theses objects. But if the singleton is reset in the AnnEngine destructor, and the list of subsystems is cleared by unwinding the stack (some love for RAII) this cause a problem.
The solution here is to have an object that will be the last to be deleted, that it it's destructor will put back the singleton to nullptr. that's what PR #74 does. This is mostly a "guard" object like in some part of the new addition of the STL.
That was an... Interesting problem to trace...
Destruction of subsystems needs to use the getter to other subsystems because the core engine is inter-connected (for example, level needs to access the audio engine to set the eventual background music or static sounds in the environment.)
Setting the static singleton pointer to nullptr in the destructor will cause crash at shutdown when the stack is unwound at the destructor closing bracket '}'.
However, the cleanup is not complete if this static member becomes a dangling pointer. And trying to recreate an AnnEngine object will fail because the constructor check for this pointer to be not set.