title: Why does this shared_ptr code segfault? When do shared_ptr's free?
body: <p>I've made a SSCE as best I can. My suspicion is that the shared pointers deconstruct (free) my objects before I ask for them in main. How can I prevent this without circumventing shared pointers altogether? This is in isolated problem in a program that otherwise is greatly helped by the use of shared_ptrs. </p>
  
  <p>Test.h:</p>
  
  <pre><code>#ifndef TEST_H
  #define TEST_H
  #include &lt;memory&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class Test
  {
      public:
          Test();
          virtual ~Test();
          static std::shared_ptr&lt;Test&gt; makeTestFrom(std::string l);
          std::vector&lt;std::shared_ptr&lt;int&gt;&gt; reg_vec;
      protected:
      private:
  
  };
  
  #endif // TEST_H
  </code></pre>
  
  <p>Test.cpp</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  Test::Test():
  reg_vec()
  {
      //ctor
  }
  
  Test::~Test()
  {
      //dtor
  }
  std::shared_ptr&lt;Test&gt; Test::makeTestFrom(std::string l)
  {
      std::shared_ptr&lt;Test&gt; sp(new Test());
      std::shared_ptr&lt;int&gt; i(new int(3));
      sp-&gt;reg_vec.push_back(i);
      return sp;
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "include/Test.h"
  using namespace std;
  
   int main()
  {
      std::unique_ptr&lt;Test&gt; x(new Test());
      x-&gt;makeTestFrom("loldoesntmatter");
      std::cout &lt;&lt; x-&gt;reg_vec[0] &lt;&lt; std::endl;
      return 0;
  
  }
  </code></pre>