title: c++ lambda, cannot access variable captured by copy
body: <p>I have this simple client/server socket code that sends and receives a lambda function. Problem is on the client inside recvlambda() function, when I try to invoke the lambda after receiving it, I get seg fault @ </p>
  
  <blockquote>
    <p>printf("Hello World! %i, %i\n", x, y);</p>
  </blockquote>
  
  <p>Debugging reveals that x and y cannot be accessed, the memory address for them is bad.</p>
  
  <p>I am using gcc 4.8.1 on Ubuntu 13.10.</p>
  
  <p>I am passing x, y by copy to the lambda in the sendlambda() function. This should not seg fault. Any idea why?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;gmpxx.h&gt;
  
  using namespace std;
  
  typedef int (*func)();
  
  /* Server code in C */
  
    #include &lt;sys/types.h&gt;
    #include &lt;sys/socket.h&gt;
    #include &lt;netinet/in.h&gt;
    #include &lt;arpa/inet.h&gt;
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;string.h&gt;
    #include &lt;unistd.h&gt;
  #include &lt;functional&gt;
  
  void sendlambda(int ConnectFD)
  {
      int x = 2342342;
      int y = 23454234;
      function&lt;int (void)&gt; f = [x, y]() mutable -&gt; int
      {
         printf("Hello World! %i, %i\n", x, y);
      };
      printf("sending lambda of %i bytes\n", sizeof(f));
      write(ConnectFD, (void*)&amp;f, sizeof(f));
  }
  
  
  void recvlambda(int SocketFD)
  {
      char buffer[1024];
      read(SocketFD, (void*)buffer, sizeof(buffer));
      function&lt;int (void)&gt; &amp;f = *(function&lt;int (void)&gt;  *)buffer;
      f();
  }
  
  int server()
  {
      printf("server\n");
      struct sockaddr_in stSockAddr;
      int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      if(-1 == SocketFD)
      {
          printf("can not create socket\n");
          exit(EXIT_FAILURE);
      }
  
      memset(&amp;stSockAddr, 0, sizeof(stSockAddr));
  
      stSockAddr.sin_family = AF_INET;
      stSockAddr.sin_port = htons(1100);
      stSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  
      if(-1 == bind(SocketFD,(struct sockaddr *)&amp;stSockAddr, sizeof(stSockAddr)))
      {
          printf("error bind failed\n");
          close(SocketFD);
          exit(EXIT_FAILURE);
      }
  
      if(-1 == listen(SocketFD, 10))
      {
          printf("error listen failed\n");
          close(SocketFD);
          exit(EXIT_FAILURE);
      }
  
      for(;;)
      {
          int ConnectFD = accept(SocketFD, NULL, NULL);
  
          if(0 &gt; ConnectFD)
          {
              printf("error accept failed\n");
              close(SocketFD);
              exit(EXIT_FAILURE);
          }
  
          /* perform read write operations ...*/
          sendlambda(ConnectFD);
  
          if (-1 == shutdown(ConnectFD, SHUT_RDWR))
          {
              printf("can not shutdown socket\n");
              close(ConnectFD);
              close(SocketFD);
              exit(EXIT_FAILURE);
          }
          close(ConnectFD);
      }
  
      close(SocketFD);
      return EXIT_SUCCESS;
  }
  
  
  int client()
  {
      printf("client\n");
      struct sockaddr_in stSockAddr;
      int Res;
      int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      if (-1 == SocketFD)
      {
          printf("cannot create socket\n");
          exit(EXIT_FAILURE);
      }
  
      memset(&amp;stSockAddr, 0, sizeof(stSockAddr));
  
      stSockAddr.sin_family = AF_INET;
      stSockAddr.sin_port = htons(1100);
      Res = inet_pton(AF_INET, "127.0.0.1", &amp;stSockAddr.sin_addr);
  
      if (0 &gt; Res)
      {
          printf("error: first parameter is not a valid address family\n");
          close(SocketFD);
          exit(EXIT_FAILURE);
      }
      else if (0 == Res)
      {
          printf("char string (second parameter does not contain valid ipaddress\n)");
          close(SocketFD);
          exit(EXIT_FAILURE);
      }
  
      if (-1 == connect(SocketFD, (struct sockaddr *)&amp;stSockAddr, sizeof(stSockAddr)))
      {
          printf("connect failed\n");
          close(SocketFD);
          exit(EXIT_FAILURE);
      }
  
      /* perform read write operations ... */
      recvlambda(SocketFD);
  
      (void) shutdown(SocketFD, SHUT_RDWR);
  
      close(SocketFD);
      return EXIT_SUCCESS;
  }
  
  int main(int argc, char** argv)
  {
      if(argc&gt;1 &amp;&amp; strcmp(argv[1], "server")==0)
          server();
      else
          client();
      return 0;
  }
  </code></pre>