title: What happens if a rvalue reference goes out of scope?
body: <p>I'm experimenting with move semantics and I am wondering what happens if a rvalue refernece goes out of scope. With following code i get runtime problems if I std::move an lvalue into </p>
  
  <pre><code>function(T t) with t = std::move(lvalue) --&gt; SEGFAULT OR double free
  </code></pre>
  
  <p>but not into </p>
  
  <pre><code>function(T &amp;&amp;t) with t = std::move(lvalue) --&gt; OK
  </code></pre>
  
  <p>Does anybody know why?</p>
  
  <p>Also, if you swap the two code-blocks in main() you get a different runtime error 0_o</p>
  
  <pre><code>// Compile with:
  // g++ move_mini.cpp -std=c++11 -o move_mini
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;algorithm&gt;
  #include &lt;list&gt;
  #include &lt;utility&gt;
  using namespace std;
  
  int num_copied;
  
  class T{
      public:
      T() : a(nullptr), b(nullptr){};
  
      T(const T &amp;t) : a(new string(*t.a)), 
                      b(new string(*t.b)){
          num_copied++;
          };
  
      T(T &amp;&amp;t){
          *this = move(t);
          };
  
      T(string s1, string s2){
          this-&gt;a = new string(s1);
          this-&gt;b = new string(s2);
          };
  
      ~T(){
          delete this-&gt;a;
          delete this-&gt;b;
          };
  
      T&amp; operator=(const T &amp;lhs){
          num_copied++;
          delete this-&gt;a;
          delete this-&gt;b;
          this-&gt;a = new string(*lhs.a);
          this-&gt;b = new string(*lhs.b);
          return *this;
          };
  
      T&amp; operator=(T &amp;&amp;lhs){
          swap(this-&gt;a, lhs.a);
          swap(this-&gt;b, lhs.b);
          return *this;
          };
  
      string *a;
      string *b;
      };
  
  void modify1(T t){
      }
  
  void modify3(T &amp;&amp;t){
      }
  
  int main(){
      cout &lt;&lt; "##### modify1(T t) #####" &lt;&lt; endl;
      T t_mv1("e", "asdsa");
      num_copied = 0;
      modify1(move(t_mv1));
      cout &lt;&lt; "t = move(t_mv)          copies " &lt;&lt; num_copied &lt;&lt; " times." &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      cout &lt;&lt; "##### modify3(T &amp;&amp;t) #####" &lt;&lt; endl;
      T t_mv3("e", "aseeferf");
      num_copied = 0;
      modify3(move(t_mv3));
      cout &lt;&lt; "t = move(t_mv)          copies " &lt;&lt; num_copied &lt;&lt; " times." &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      return 0;
      }
  </code></pre>