title: Boost Fusion container of shared pointers (shared_ptr) causing Segmentation Fault (sigsegv) or garbage results
body: <p><strong>Edit: This turned out to be an issue with temporaries.  Basically, I was ignorantly using C++ as if it worked like Java or C#, which it does not.  Hopefully, this will be a good cautionary tale.</strong></p>
  
  <p>Edit: This issue only seems to happen with the combination of <code>joint_view</code> and <code>shared_ptr</code>.  Raw pointers seem to work fine in the same scenario, as do shared pointers in a plain fusion container constructed w/ all its items at once, without adding anything more to it.  Details below:</p>
  
  <p><em>I'm using mingw gcc 4.5.1</em>
  Running into a peculiar issue when using boost fusion container(s) and getting contents back out.  I have a custom class that gets wrapped in a <code>std::shared_ptr</code>, then that's handed to a fusion <code>make_list()</code> (or <code>make_vector()</code>, doesn't seem to matter).  All is well if I can get all of my objects into the container at one time.  The problem seems to come up when I add another shared pointer to the container, which yields a <code>joint_view</code>.  I iterate using a <code>fusion::for_each()</code> and pass in a function object to print out the value.  If I'm iterating a plain fusion container of shared pointers instead of a <code>joint_view</code>, or a <code>joint_view</code> with no shared pointers in it, it works fine, but otherwise, segmentation fault or garbage values.</p>
  
  <p>Below is a test program I made to try and isolate my problem.  Any ideas on what the issue may be?  It's entirely possible I'm just missing something that I should/shouldn't be doing :(</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  //BOOST SMART POINTERS
  //I only use boost's shared_ptr for ONE test, results are the same.
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/make_shared.hpp&gt;
  
  //BOOST FUSION
  #include &lt;boost/fusion/algorithm/iteration/for_each.hpp&gt;
  #include &lt;boost/fusion/include/for_each.hpp&gt;
  #include &lt;boost/fusion/container/generation/make_list.hpp&gt;
  #include &lt;boost/fusion/include/make_list.hpp&gt;
  #include &lt;boost/fusion/container/generation/make_vector.hpp&gt;
  #include &lt;boost/fusion/include/make_vector.hpp&gt;
  #include &lt;boost/fusion/algorithm/transformation/push_back.hpp&gt;
  #include &lt;boost/fusion/include/push_back.hpp&gt;
  
  using namespace std;
  using namespace boost::fusion;
  
  struct TestStructInt {
      int test_int;
  
      TestStructInt(int init_num) : test_int(init_num) {};
  };
  
  struct TestStructString {
      string test_string;
  
      TestStructString(string init_str) : test_string(init_str) {};
  };
  
  struct do_junk{
      void operator()(string t) const {
          cout &lt;&lt; t &lt;&lt; endl;
      }
  
      void operator()(string* t) const {
          cout &lt;&lt; *t &lt;&lt; endl;
      }
  
      void operator()(std::shared_ptr&lt;int&gt; t) const {
          cout &lt;&lt; *t &lt;&lt; endl;
      }
  
      void operator()(std::shared_ptr&lt;string&gt; t) const {
          cout &lt;&lt; *t &lt;&lt; endl;
      }
  
      void operator() (boost::shared_ptr&lt;string&gt; t) const {
          cout &lt;&lt; *t &lt;&lt; endl;
      }
  
      void operator() (TestStructInt t) const {
          cout &lt;&lt; t.test_int &lt;&lt; endl;
      }
  
      void operator() (std::shared_ptr&lt;TestStructInt&gt; t) const {
          cout &lt;&lt; t-&gt;test_int &lt;&lt; endl;
      }
  
      void operator() (TestStructString t) const {
          cout &lt;&lt; t.test_string &lt;&lt; endl;
      }
  
      void operator() (std::shared_ptr&lt;TestStructString&gt; t) const {
          cout &lt;&lt; t-&gt;test_string &lt;&lt; endl;
      }
  
      void operator() (std::shared_ptr&lt;TestStructString*&gt; t) const {
          cout &lt;&lt; (*t)-&gt;test_string &lt;&lt; endl;
      }
  };
  
  int main()
  {
      string foo1 = "foo1";
      string foo2 = "foo2";
      string foo3 = "foo3";
      int bar1 = 1;
      int bar2 = 2;
      int bar3 = 3;
      string* foo1_ptr = &amp;foo1;
      string* foo2_ptr = &amp;foo2;
      string* foo3_ptr = &amp;foo3;
      auto foo1_obj = make_shared&lt;TestStructString&gt;(TestStructString("foo1"));
      auto foo2_obj = make_shared&lt;TestStructString&gt;(TestStructString("foo2"));
      auto foo3_obj = make_shared&lt;TestStructString&gt;(TestStructString("foo3"));
  
      //works fine
      auto list_test1 = make_list(foo1, foo2);
  
      //works fine
      auto list_test2 = make_list(foo1_ptr, foo2_ptr);
  
      //seems to work, but is undefined behavior
      auto list_test3
          = make_list(
              std::make_shared&lt;int&gt;(bar1), std::make_shared&lt;int&gt;(bar2)
          )
      ;
  
      //seems to work, but is undefined behavior
      auto list_test4
          = make_list(
              std::make_shared&lt;string&gt;(foo1), std::make_shared&lt;string&gt;(foo2)
          )
      ;
  
      //seems to work, but is undefined behavior
      auto list_test5
          = make_list(
              std::make_shared&lt;TestStructInt&gt;(TestStructInt(1))
              , std::make_shared&lt;TestStructInt&gt;(TestStructInt(2))
          )
      ;
  
      //seems to work, but is undefined behavior
      auto list_test6
          = make_list(
              std::make_shared&lt;TestStructString&gt;(TestStructString("foo1"))
              , std::make_shared&lt;TestStructString&gt;(TestStructString("foo2"))
          )
      ;
  
      //seems to work, but is undefined behavior
      auto list_test7
          = make_list(TestStructString("foo1"), TestStructString("foo2"))
      ;
  
      //seems to work, but is undefined behavior
      auto joint_view_test1 = push_back(make_list(foo1, foo2), foo3);
  
      //seems to work, but is undefined behavior
      auto joint_view_test2 = push_back(make_list(foo1_ptr, foo2_ptr), foo3_ptr);
  
      //seems to work, but is undefined behavior
      auto joint_view_test3
          = push_back(
              make_list(
                  TestStructString(foo1), TestStructString(foo2)
              )
              , TestStructString(foo3)
          )
      ;
  
      //integer values I pass in are coming out different
      auto joint_view_test4
          = push_back(
              make_list(
                  std::make_shared&lt;int&gt;(bar1), std::make_shared&lt;int&gt;(bar2)
              )
              , make_shared&lt;int&gt;(bar3)
          )
      ;
  
      //pass in foo1, foo2, and foo3, but only get foo3's value back out for each
      auto joint_view_test5
          = push_back(
              make_list(
                  std::make_shared&lt;string&gt;(foo1), std::make_shared&lt;string&gt;(foo2)
              )
              , make_shared&lt;string&gt;(foo3)
          )
      ;
  
      //causes seg fault when running do_junk()
      auto joint_view_test6
          = push_back(
              make_vector(
                  std::make_shared&lt;string&gt;(foo1), std::make_shared&lt;string&gt;(foo2)
              )
              , std::make_shared&lt;string&gt;(foo3)
          )
      ;
  
      //causes seg fault when running do_junk()
      auto joint_view_test7
          = push_back(
              make_list(
                  boost::make_shared&lt;string&gt;(foo1)
                  , boost::make_shared&lt;string&gt;(foo2)
              )
              , boost::make_shared&lt;string&gt;(foo3)
          )
      ;
  
      //integer values I pass in are coming out different
      auto joint_view_test8
          = push_back(
              make_list(
                  std::make_shared&lt;TestStructInt&gt;(TestStructInt(1))
                  , std::make_shared&lt;TestStructInt&gt;(TestStructInt(2))
              )
              , std::make_shared&lt;TestStructInt&gt;(TestStructInt(3))
          )
      ;
  
      //causes seg fault when running do_junk()
      auto joint_view_test9
          = push_back(
              make_list(
                  std::make_shared&lt;TestStructString&gt;(TestStructString("foo1"))
                  , std::make_shared&lt;TestStructString&gt;(TestStructString("foo2"))
              )
              , std::make_shared&lt;TestStructString&gt;(TestStructString("foo3"))
          )
      ;
  
      //causes seg fault when running do_junk()
      auto joint_view_test10
          = push_back(
              make_list(
                  std::make_shared&lt;TestStructString*&gt;(new TestStructString("foo1"))
                  , std::make_shared&lt;TestStructString*&gt;(new TestStructString("foo2"))
              )
              , std::make_shared&lt;TestStructString*&gt;(new TestStructString("foo3"))
          )
      ;
  
      //seems to work, but is undefined behavior
      auto joint_view_test11
          = push_back(
              make_list(
                  foo1_obj
                  , foo2_obj
              )
              , foo3_obj
          )
      ;
  
      cout &lt;&lt; "@@ list1" &lt;&lt; endl;
      boost::fusion::for_each(list_test1, do_junk());
      cout &lt;&lt; "@@ list2" &lt;&lt; endl;
      boost::fusion::for_each(list_test2, do_junk());
      cout &lt;&lt; "@@ list3" &lt;&lt; endl;
      boost::fusion::for_each(list_test3, do_junk());
      cout &lt;&lt; "@@ list4" &lt;&lt; endl;
      boost::fusion::for_each(list_test4, do_junk());
      cout &lt;&lt; "@@ list5" &lt;&lt; endl;
      boost::fusion::for_each(list_test5, do_junk());
      cout &lt;&lt; "@@ list6" &lt;&lt; endl;
      boost::fusion::for_each(list_test6, do_junk());
      cout &lt;&lt; "@@ list7" &lt;&lt; endl;
      boost::fusion::for_each(list_test7, do_junk());
      cout &lt;&lt; "@@ joint_view1" &lt;&lt; endl;
      boost::fusion::for_each(joint_view_test1, do_junk());
      cout &lt;&lt; "@@ joint_view2" &lt;&lt; endl;
      boost::fusion::for_each(joint_view_test2, do_junk());
      cout &lt;&lt; "@@ joint_view3" &lt;&lt; endl;
      boost::fusion::for_each(joint_view_test3, do_junk());
      cout &lt;&lt; "@@ joint_view4" &lt;&lt; endl;
      boost::fusion::for_each(joint_view_test4, do_junk());
      cout &lt;&lt; "@@ joint_view5" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test5, do_junk());
      cout &lt;&lt; "@@ joint_view6" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test6, do_junk());
      cout &lt;&lt; "@@ joint_view7" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test7, do_junk());
      cout &lt;&lt; "@@ joint_view8" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test8, do_junk());
      cout &lt;&lt; "@@ joint_view9" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test9, do_junk());
      cout &lt;&lt; "@@ joint_view10" &lt;&lt; endl;
      //boost::fusion::for_each(joint_view_test10, do_junk());
      cout &lt;&lt; "@@ joint_view11" &lt;&lt; endl;
      boost::fusion::for_each(joint_view_test11, do_junk());
      cout &lt;&lt; "@@" &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>