Skip to content

Commit f915d83

Browse files
committed
lwipClient - fix mem pool leak with shared_ptr
1 parent 795a81d commit f915d83

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

libraries/lwIpWrapper/src/lwipClient.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ lwipClient::lwipClient(uint8_t sock)
2525
/* -------------------------------------------------------------------------- */
2626
lwipClient::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

libraries/lwIpWrapper/src/lwipClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lwipMem.h"
1010
#include "lwipTcp.h"
1111
#include "lwipTypes.h"
12+
#include <memory>
1213

1314
class lwipClient : public Client {
1415

@@ -66,7 +67,7 @@ class lwipClient : public Client {
6667
using Print::write;
6768

6869
private:
69-
struct tcp_struct* _tcp_client;
70+
std::shared_ptr<struct tcp_struct> _tcp_client;
7071
uint16_t _timeout = 10000;
7172
};
7273

0 commit comments

Comments
 (0)