   Game State Changes and Child Window Destruction
  I've got two questions, both of which I are pretty basic, but I feel they are important to get right in order to design a good program.  So here goes.Game State ChangesRight now I have a main game state stack which I push new game states onto, and then I pop the top game state (which is the active game state) when the user clicks a "back" button.  This allows me to very easily change the game state to a new state when a certain menu button is pushed, or to go back to a previous game state (for example, going from an in-game state back to the main-menu state by selecting "quit").  While I have a working method, I don't like it a whole lot.  I would like to design a new system by using a new class GameStateListener which a class can derive from that would easily allow a game state to tell its listener(s) that the game state needs to change (and what to change to).  However, there is a problem with this method, which I describe below.  How can I overcome this problem?// the back button was selected, so tell the game state listener to pop this game state in order to go back to the previous game statevoid GameState::BackButtonSelected(){    // m_gameStateListener is a game state listener, which in my case would    // also be a class containing the main game state stack, and thus contains    // whatever game state the "this" pointer points to    // calling changeGameState() tells the listener to change the game state to    // something new   // changeGameState() accepts an enum which tells what the new game state    // is, which in this case is GAME_STATE_BACK, whic basically tells the game    // state listener to pop the top game state in order to go back a game state    m_gameStateListener->changeGameState(GAME_STATE_BACK);   // the problem though is that whatever game state calls m_gameStateListener->changeGameState(GAME_STATE_BACK)    // is also the active game state, so if the game state listener pops the    // active game state, it deletes this game state before this function has returned   // how do I fix this problem???}Destroying a Child WindowIn my simple game engine, I can create a window and a child window by doing the following:int main(){    // create a new root    engine::Root* root = new engine::Root();   // tell the root to create the main render window    // note that engine::RenderWindowCreationParameters isn't an actual type name, but I use it just to show you that I pass in some creation parameters    engine::RenderWindow* mainWindow = root->createRenderWindow(engine::RenderWindowCreationParameters);   // tell the main render window to create a child render window    engine::RenderWindow* childWindow = mainWindow->createChildRenderWindow(engine::RenderWindowCreationParameters);   // game junk goes here   // childWindow is no longer needed, so we destroy it    childWindow->destroy();   // more game junk here   // clean up    delete root;}I don't like that though because calling childWindow->destroy() destroys the window, and then sends a message to the parent window saying that the child was destroyed, so then the parent window deletes the child window.  Would it be better to have childWindow->destroy() destroy the window and not send a message to the parent window to delete childWindow, but then have another function to tell a window to delete a child (i.e. create a method such that I can do mainWindow->deleteChildRenderWindow(childWindow), which physically deletes childWindow, but calling childWindow->destroy() destroys the window but doesn't delete childWindow)?  I think I like this a lot more, but thought I'd see what everyone here thought about it.   