title: Shared pointers to a singleton do not recognize each other
body: <p>I am currently learning how to use the C++11 smart pointers while programming a 2D game engine as a hobby using SDL. However, I ran into a problem while implementing an OOp wrapper for SDL.</p>
  
  <p>The intent is to create a singleton class, which initializes SDL when it is constructed, and shuts SDL down when it gets destroyed. The singleton class has a static method <code>getInstance</code> that returns a <code>shared_ptr</code> to the singleton, and constructs the single instance if no instance exists, the idea being that all clients of the singleton own a <code>shared_ptr</code> to it, and when all clients get destroyed, the singleton is also destroyed. I do understand that singletons (and other globals) are generally bad, but I think that this could be one of the few cases where a singleton is appropriate, as there can only be one SDL library in use.</p>
  
  <p>The problem lies in returning the <code>shared_ptr</code> from the <code>getInstance</code> method. Instead of using the same <code>shared_ptr</code> manager object, the <code>shared_ptr</code> instances are unrelated, and destroying a single one of them deallocates the singleton.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  using namespace std;
  
  class Foo
  {
  public:
      ~Foo(){cout &lt;&lt; "Foo &lt;" &lt;&lt; this &lt;&lt; "&gt; destroyed\n"; instance_ = nullptr;}
      static shared_ptr&lt;Foo&gt; getInstance()
      {
          if(instance_ == nullptr)
              instance_ = new Foo;
          //problem: the shared pointers created are unaware of each other
          return shared_ptr&lt;Foo&gt;(instance_);
      }
  private:
      Foo(){cout &lt;&lt; "Foo &lt;" &lt;&lt; this &lt;&lt; "&gt; constructed\n";}
      Foo(Foo&amp; other){}
      void operator=(Foo&amp; other){}
      static Foo* instance_;
  };
  
  Foo* Foo::instance_ = nullptr;
  
  int main()
  {
      shared_ptr&lt;Foo&gt; a = Foo::getInstance();
      shared_ptr&lt;Foo&gt; b = Foo::getInstance();
      shared_ptr&lt;Foo&gt; c = Foo::getInstance();
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Foo &lt;0x3e2a10&gt; constructed
  Foo &lt;0x3e2a10&gt; destroyed
  Foo &lt;0x3e2a10&gt; destroyed
  Foo &lt;0x3e2a10&gt; destroyed
  </code></pre>