title: Why this pointer captured by lambda is corrupted while other variable is not
body: <p>I have compiled (g++ -std=c++11 a.cpp) and run following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  class A {
      std::function&lt;void(void)&gt; f;
  public:
      A(std::function&lt;void(void)&gt; pf) : f(pf) {}
      void callf() { f(); }
  };
  
  class B {
      A *a;
  public:
      void test() {
          B *that = this;
          auto f = [this, that]() {
              cout &lt;&lt; "this: " &lt;&lt; this &lt;&lt; " that: " &lt;&lt; that &lt;&lt; endl;
              delete this-&gt;a;
              cout &lt;&lt; "this: " &lt;&lt; this &lt;&lt; " that: " &lt;&lt; that &lt;&lt; endl;
          };
  
          a = new A(f);
          a-&gt;callf();
      }
  };
  
  int main()
  {
      B().test();
  }
  </code></pre>
  
  <p>Its output was:</p>
  
  <pre><code>this: 0x7fff158c88f0 that: 0x7fff158c88f0
  this: 0x1ea3000 that: 0x7fff158c88f0
  </code></pre>
  
  <p>I do not understand why captured this changed its value while identical pointer with different name was not corrupted.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I understand that lambda is destroyed and I am producing UB but what I do not understand is why one variable is corrupted while other not. I do not understand low level details behind this behaviour which is by the way very consistent. I want to understand gcc implementation details that cause this kind of behaviour.</p>
  
  <p>My further investigation showed that changing <code>auto f = [this, that]</code> to <code>auto f = [that, this]</code> caused that to be corrupted.</p>