title: lwIP: How to avoid "Memory Error (ERR_MEM) " in applications which needs continously Connection-Disconnection process?
body: <p>I have an embedded TCP Client application which using lwIP TCP/IP stack and raw API in the local network. The device continuously polls the server devices in every 2 secs. The scenario is like in below:</p>
  
  <ul>
  <li>The client connects to the server device.</li>
  <li>It sends a query.</li>
  <li>It receives a response from the server.</li>
  <li>It parses and processing data.</li>
  <li>The client closes the connection to connect another server device with a different IP.</li>
  <li>Goes back to the first step and state machine continuously runs in that scenario.</li>
  </ul>
  
  <p>After some time (300 polling) when I call <i>tcp_connect</i> function, it returns <i>ERR_MEM</i> memory error. After that, I am not able to connect server devices. It is like my application is getting all memory over after continuously connection-disconnection process because it always happens after a certain number of a poll. </p>
  
  <p>I analyzed the sending/receiving frames in Wireshark and connection/disconnection process works as expected. In 3rd party TCP Server software, I checked whether the disconnection process works correctly or not.</p>
  
  <p>I need some recommendations to avoid memory error in that scenario.  </p>
  
  <p>Best Regards</p>
  
  <pre class="lang-c prettyprint-override"><code>struct tcp_pcb *pxPCBMaster;
  struct sTCPMasterClient sTCPMasterClient;
  
  /*
  * This function continously runs in super loop.
  * It connects and disconnects successfully but after some time
  * tcp_connect function returns ERR_MEM.
  */
  
  void vTCPSocketTestFunc (void)
  {
     pxPCBMaster = tcp_new(); 
  
     ip_addr_t xDestIPAddress;
  
     IP4_ADDR (&amp;xDestIPAddress, 192, 168, 10, 7);
  
     // Connect to test server
     err_t eConnStatus = tcp_connect (pxPCBMaster, 
                                     &amp;xDestIPAddress, 
                                     80, 
                                     prveMBTCPMPortConnectionCallback);
     if (eConnStatus != ERR_OK)
     {
         printf("DEBUG_MBTCPM_PORT: TCP Connection Error Occured\n");
         printf("DEBUG_MBTCPM_PORT: Exception Code: %d\n", eConnStatus);
     }
  
     else
     {
        printf("DEBUG_MBTCPM_PORT: TCP Connection is successful\n");
  
        // Refresh Ethernet Driver and Add 2 secs Delay
        for (int i = 0; i &lt; 200; i++)
        {
           ethernetif_input(&amp;tcpNetif);
           HAL_Delay (10);
        }
  
        if (sTCPMasterClient != NULL)
        {
           mem_free(sTCPMasterClient );
        }
  
        // Close connection
        tcp_close(pxPCBMaster);
  
        // Aborts the connection by sending a RST (reset) segment to the remote 
        // host. The pcb is deallocated
        tcp_abort(pxPCBMaster);
  
        // Refresh Ethernet Driver and Add 2 secs Delay
        for (int i = 0; i &lt; 200; i++)
        {
           ethernetif_input(&amp;tcpNetif);
           HAL_Delay (10);
        }
     }
  }
  </code></pre>