title: Double Delete using Shared Pointers, even though there is still a shared_ptr to the object, its destructor is being called
body: <p>I apologize if this is a lot of code to read, if I can simplify with explanations please let me know, also if you'd like to comment on my design/practices feel free.</p>
  
  <p>So my Player is being deleted twice, I'm not sure why. If you look at the call stack, you'll see that GameEventManager is actually calling the Player destructor before the GameState destructor, even though GameState is the one with the pointer to Player. I'm thinking maybe this is because it's also destroying the vector first, so it finds Player in the vector and tries to destroy it. I don't know why it would try to destroy the Player though, since there is still a reference to Player that the GameState object knows about. The shared pointer functionality should prevent the Player from being destroyed.</p>
  
  <p>Maybe I am going about it the wrong way, if so could someone point me in the correct direction for best practices here? Thanks</p>
  
  <p>Call stack:</p>
  
  <p><img src="https://i.stack.imgur.com/wRwaZ.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/zSXDt.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/diDh1.png" alt="enter image description here">
  GameState.h</p>
  
  <pre><code>#ifndef _level
  #define _level
  #include &lt;vector&gt;
  #include "Player.h"
  #include &lt;memory&gt;
  
  using namespace std;
  
  class GameState // A representation of the Level/Game/Scene
  {
  public:
      GameState();
      virtual ~GameState() {}
      //Keep track of the game's state
  
      //Maybe get rid of the Level class, and make this a class, and move the Level functionality here?
  
      static void EndGame(const bool &amp; b) { mbEndGame = b; }
  
      static const bool &amp; EndGame() { return mbEndGame; }
  private:
      void SetPlayer(shared_ptr&lt;Player&gt; sptrPlayer) { msptrPlayer = sptrPlayer; }
      static bool mbEndGame;
      shared_ptr&lt;Player&gt; msptrPlayer;
      vector&lt;shared_ptr&lt;Player&gt;&gt; msptrMob; // Representation of all the NPCs in the game. Eventually make a Mob class but for now just use Player
      // shared_ptr&lt;LevelMap&gt; mMap // Representation of what the game looks like visually
      // Renderer // Should the level have the renderer to create the graphics? Or should this be handled by another "GUI Layer" and interact with this layer as little as possible ?
  };
  
  #endif
  </code></pre>
  
  <p>GameState.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "GameState.h"
  
  bool GameState::mbEndGame(false);
  
  GameState::GameState() : msptrPlayer(NULL)
  {
      shared_ptr&lt;Player&gt; sptrPlayer(new Player);
      SetPlayer(sptrPlayer);
  }
  GameObject.h
  #ifndef _game_object
  #define _game_object
  #include &lt;memory&gt;
  
  // Game Object Classes inherit this so they are registered with GameEventManager.
  using namespace std;
  class GameObject{
  public:
  
      GameObject();
      virtual ~GameObject() {}
      virtual void Update() = 0;
      virtual void Start() = 0;
  
  
  };
  #endif
  GameObject.cpp
  
  #include "stdafx.h"
  #include "GameObject.h"
  #include "GameEventManager.h"
  
  GameObject::GameObject()
  {
      shared_ptr&lt;GameObject&gt; ptr(this);
      GameEventManager::GetGameEventManager()-&gt;RegisterGameObject(ptr);
  }
  </code></pre>
  
  <p>Player.h</p>
  
  <pre><code>#ifndef _player
  #define _player
  
  #include "GameObject.h"
  //A representation of the Player. Used by Level.
  class Player : public GameObject
  {
  public:
      Player();
      virtual ~Player() {}
  private:
      virtual void Update();
      virtual void Start();
      int miMaxHealth;
  
  };
  #endif
  </code></pre>
  
  <p>Player.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "Player.h"
  #include "GameState.h"
  
  Player::Player() : miMaxHealth(100) {};
  void
  Player::Start()
  {
  
  }
  
  void
  Player::Update() // reimplement GameObject::Update(), which is called by the GameEventManager
  {
      --miMaxHealth;
      if (miMaxHealth &lt;= 0)
      {
          GameState::EndGame(true);
      }
  }
  </code></pre>
  
  <p>GameEventManager.h</p>
  
  <pre><code>#ifndef _game_event_manager
  #define _game_event_manager
  
  #include "stdafx.h"
  #include &lt;memory&gt;
  #include &lt;vector&gt;
  #include "GameObject.h"
  #include "GameState.h"
  
  using namespace std;
  
  class GameEventManager{ 
      //Object which inherit from GameObject are automatically registered with GameEventManager when they are constructed.
      // GameEventManager creates the level object to represent the game, and then runs Start() on all registered GameObjects
      // and then continually runs Update() on all registered game objects until the GameState is set to EndGame.
  public:
      virtual ~GameEventManager(){} // This gets called at the end of the program (I guess whne static variables are destroyed), and crashes during vector&lt;shared pointer &lt;GameObject&gt;&gt; destruction, probably because
                                    // Player already destroyed it. So... not sure what to do. If I make it non-static  
  
      void StartGameEvents(); 
      const static shared_ptr&lt;GameEventManager&gt;&amp; GetGameEventManager();
      const shared_ptr&lt;GameState&gt;&amp; GetLevel();
      void RegisterGameObject(shared_ptr&lt;GameObject&gt; sptrGameObject);
  
      const shared_ptr&lt;vector&lt;shared_ptr&lt;GameObject&gt;&gt;&gt;&amp; GetRegisteredGameVector() const { return mvecRegisteredGameVector; }
  
  private:
      GameEventManager(); //singleton
      void AddGameObject(shared_ptr&lt;GameObject&gt;);
      shared_ptr&lt;GameState&gt; mLevel;
      shared_ptr&lt;vector&lt;shared_ptr&lt;GameObject&gt;&gt;&gt; mvecRegisteredGameVector; //Reference because shared pointer will double delete otherwise. ~Level() still deletes it but this way I guess it doesn't try to delete again? but...
                                                                          //Now I'm trying it as a shared_ptr, but it's not working. ~Level() still deletes it even though there is a shared pointer to a vector pointing to the Player. Why is ~Level() doing this?
  
      static shared_ptr&lt;GameEventManager&gt; msptrGameEventManager;
  };
  #endif
  </code></pre>
  
  <p>GameEventManager.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "GameEventManager.h"
  #include "GameState.h"
  
  shared_ptr&lt;GameEventManager&gt; GameEventManager::msptrGameEventManager(new GameEventManager);
  
  void
  GameEventManager::StartGameEvents()
  {
      //run once
      int size = GetRegisteredGameVector()-&gt;size();
      vector&lt;shared_ptr&lt;GameObject&gt;&gt; &amp; vecsptrRegisteredGameVector = (*GetRegisteredGameVector());
      for (int i = 0; i &lt; GetRegisteredGameVector()-&gt;size(); ++i)
      {
          vecsptrRegisteredGameVector[i]-&gt;Start(); //nothing for now 
      }
      //keep running
      while (GetLevel()-&gt;EndGame() != true)
      {
          for (int i = 0; i &lt; GetRegisteredGameVector()-&gt;size(); i++)
          {
              (*GetRegisteredGameVector())[i]-&gt;Update(); //Player's life goes from 100 to zero, see Player::Update
  
          }
      }
      return; 
      // GameState destructor is called and destroys player for some reason, even though it's still being referenced by the GameEventManager's vector.
  }
  
  GameEventManager::GameEventManager() : mvecRegisteredGameVector(new vector&lt;shared_ptr&lt;GameObject&gt;&gt;) , mLevel(NULL) //Instantiating the level before the GameEventManager is fully instantiated causes an infinite recursion.
  {
      return;
  }
  
  const shared_ptr&lt;GameEventManager&gt;&amp; 
  GameEventManager::GetGameEventManager()
  {
      if (!msptrGameEventManager)
      {
          msptrGameEventManager.reset(new GameEventManager);
      }
      return msptrGameEventManager;
  }
  
  const shared_ptr&lt;GameState&gt;&amp;
  GameEventManager::GetLevel()
  {
      if (!mLevel)
      {
          mLevel.reset(new GameState);
      }
      return mLevel;
  }
  void 
  GameEventManager::RegisterGameObject(shared_ptr&lt;GameObject&gt; sptrGameObject)
  {
      GetGameEventManager()-&gt;AddGameObject(sptrGameObject);
  }
  void
  GameEventManager::AddGameObject(shared_ptr&lt;GameObject&gt; sptrGameObject)
  {
      GetRegisteredGameVector()-&gt;push_back(sptrGameObject);
  }
  </code></pre>