@@ -25,7 +25,10 @@ lwipClient::lwipClient(uint8_t sock)
2525/* -------------------------------------------------------------------------- */
2626lwipClient::lwipClient (struct tcp_struct * tcpClient)
2727{
28- _tcp_client = tcpClient;
28+ if (tcpClient == NULL )
29+ return ;
30+ _tcp_client.reset (tcpClient,
31+ [](struct tcp_struct *tcp_client) { (void ) tcp_client; } ); // empty deleter
2932}
3033/* -------------------------------------------------------------------------- */
3134
@@ -49,7 +52,8 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
4952 /* -------------------------------------------------------------------------- */
5053 if (_tcp_client == NULL ) {
5154 /* Allocates memory for client */
52- _tcp_client = (struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct ));
55+ _tcp_client.reset ((struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct )),
56+ [](struct tcp_struct *tcp_client) { mem_free (tcp_client); } );
5357
5458 if (_tcp_client == NULL ) {
5559 return 0 ;
@@ -69,7 +73,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
6973
7074 uint32_t startTime = millis ();
7175 ip_addr_t ipaddr;
72- tcp_arg (_tcp_client->pcb , _tcp_client);
76+ tcp_arg (_tcp_client->pcb , _tcp_client. get () );
7377 if (ERR_OK != tcp_connect (_tcp_client->pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port, &tcp_connected_callback)) {
7478 stop ();
7579 return 0 ;
@@ -215,7 +219,7 @@ void lwipClient::stop()
215219
216220 // close tcp connection if not closed yet
217221 if (status () != TCP_CLOSING) {
218- tcp_connection_close (_tcp_client->pcb , _tcp_client);
222+ tcp_connection_close (_tcp_client->pcb , _tcp_client. get () );
219223 }
220224}
221225
0 commit comments