@@ -25,7 +25,10 @@ lwipClient::lwipClient(uint8_t sock)
25
25
/* -------------------------------------------------------------------------- */
26
26
lwipClient::lwipClient (struct tcp_struct * tcpClient)
27
27
{
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
29
32
}
30
33
/* -------------------------------------------------------------------------- */
31
34
@@ -49,7 +52,8 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
49
52
/* -------------------------------------------------------------------------- */
50
53
if (_tcp_client == NULL ) {
51
54
/* 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); } );
53
57
54
58
if (_tcp_client == NULL ) {
55
59
return 0 ;
@@ -69,7 +73,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
69
73
70
74
uint32_t startTime = millis ();
71
75
ip_addr_t ipaddr;
72
- tcp_arg (_tcp_client->pcb , _tcp_client);
76
+ tcp_arg (_tcp_client->pcb , _tcp_client. get () );
73
77
if (ERR_OK != tcp_connect (_tcp_client->pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port, &tcp_connected_callback)) {
74
78
stop ();
75
79
return 0 ;
@@ -215,7 +219,7 @@ void lwipClient::stop()
215
219
216
220
// close tcp connection if not closed yet
217
221
if (status () != TCP_CLOSING) {
218
- tcp_connection_close (_tcp_client->pcb , _tcp_client);
222
+ tcp_connection_close (_tcp_client->pcb , _tcp_client. get () );
219
223
}
220
224
}
221
225
0 commit comments