@@ -8,7 +8,6 @@ extern "C" {
88
99/* -------------------------------------------------------------------------- */
1010lwipClient::lwipClient ()
11- : _tcp_client(NULL )
1211{
1312}
1413/* -------------------------------------------------------------------------- */
@@ -17,15 +16,17 @@ lwipClient::lwipClient()
1716sketches but sock is ignored. */
1817/* -------------------------------------------------------------------------- */
1918lwipClient::lwipClient (uint8_t sock)
20- : _tcp_client(NULL )
2119{
2220}
2321/* -------------------------------------------------------------------------- */
2422
2523/* -------------------------------------------------------------------------- */
2624lwipClient::lwipClient (struct tcp_struct * tcpClient)
2725{
28- _tcp_client = tcpClient;
26+ if (tcpClient == NULL )
27+ return ;
28+ _tcp_client.reset (tcpClient,
29+ [](struct tcp_struct *tcp_client) { (void ) tcp_client; } ); // empty deleter
2930}
3031/* -------------------------------------------------------------------------- */
3132
@@ -49,7 +50,8 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
4950 /* -------------------------------------------------------------------------- */
5051 if (_tcp_client == NULL ) {
5152 /* Allocates memory for client */
52- _tcp_client = (struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct ));
53+ _tcp_client.reset ((struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct )),
54+ [](struct tcp_struct *tcp_client) { mem_free (tcp_client); } );
5355
5456 if (_tcp_client == NULL ) {
5557 return 0 ;
@@ -69,7 +71,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
6971
7072 uint32_t startTime = millis ();
7173 ip_addr_t ipaddr;
72- tcp_arg (_tcp_client->pcb , _tcp_client);
74+ tcp_arg (_tcp_client->pcb , _tcp_client. get () );
7375 if (ERR_OK != tcp_connect (_tcp_client->pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port, &tcp_connected_callback)) {
7476 stop ();
7577 return 0 ;
@@ -215,7 +217,7 @@ void lwipClient::stop()
215217
216218 // close tcp connection if not closed yet
217219 if (status () != TCP_CLOSING) {
218- tcp_connection_close (_tcp_client->pcb , _tcp_client);
220+ tcp_connection_close (_tcp_client->pcb , _tcp_client. get () );
219221 }
220222}
221223
0 commit comments