title: Lambda machine-dependent segmentation-fault (possible compiler bug?)
body: <p>Today I came across a very strange bug. I created a minimal example:</p>
  
  <p><a href="https://gist.github.com/SuperV1234/5792381" rel="nofollow">https://gist.github.com/SuperV1234/5792381</a></p>
  
  <p>Basically, on some machines, "test 2" segfaults; on others, it works as intended.
  On my desktop computer it works, both on Windows 8 x64 and Linux Mint 15 x64.
  On my laptop computer it segfaults, both on Windows 8 x64 and Linux Mint 15 x64.</p>
  
  <p>What puzzles me is:</p>
  
  <ul>
  <li>the fact that it works on some machines and crashes on other machines</li>
  <li>the fact that simply wrapping the lambda contents in another function fixes the segfault</li>
  </ul>
  
  <p>Is this a compiler bug? Or is there a difference between <code>Game::test1()</code> and the lambda body?</p>
  
  <pre><code>// Test 1 works
  // Test 2 segfaults... on some machines.
  // Compiled with -std=c++11, GCC 4.8.1, tested both on native Linux, Windows and Wine
  
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  struct Command
  {
      function&lt;void()&gt; func;
      void reset() { }
  };
  
  struct Timeline
  {
      vector&lt;Command*&gt; commands;
      void clear() 
      {
          for(auto&amp; c : commands) delete c;
          commands.clear();
      }
      void reset() { for(auto&amp; c : commands) c-&gt;reset(); }
  };
  
  struct Game
  {
      Timeline timeline;
  
      void test1() { timeline.clear(); timeline.reset(); }
      void run()
      {
          {
              cout &lt;&lt; "Starting test 1..." &lt;&lt; endl;
  
              Command* cmd{new Command};
              cmd-&gt;func = [&amp;]{ test1(); };
              timeline.commands.push_back(cmd); cmd-&gt;func();
  
              cout &lt;&lt; "Successfully ending test 1..." &lt;&lt; endl;
          }
  
          {
              cout &lt;&lt; "Starting test 2..." &lt;&lt; endl;
  
              Command* cmd{new Command};
              cmd-&gt;func = [&amp;]{ timeline.clear(); timeline.reset(); };
              timeline.commands.push_back(cmd); cmd-&gt;func();
  
              cout &lt;&lt; "Successfully ending test 2..." &lt;&lt; endl;
          }
      }
  };
  
  int main() { Game{}.run(); return 0; }
  </code></pre>
  
  <p>Real code (not a minimal example) is available here: <a href="https://github.com/SuperV1234/SSVOpenHexagon/commit/77784ae142768f964666afacfeed74300501ec07" rel="nofollow">https://github.com/SuperV1234/SSVOpenHexagon/commit/77784ae142768f964666afacfeed74300501ec07</a></p>
  
  <p>Backtrace from real code: <a href="http://paste2.org/W7yeCxOO" rel="nofollow">http://paste2.org/W7yeCxOO</a></p>