Skip to content

Commit ebe2184

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

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

libraries/lwIpWrapper/src/lwipClient.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ extern "C" {
88

99
/* -------------------------------------------------------------------------- */
1010
lwipClient::lwipClient()
11-
: _tcp_client(NULL)
1211
{
1312
}
1413
/* -------------------------------------------------------------------------- */
@@ -17,15 +16,17 @@ lwipClient::lwipClient()
1716
sketches but sock is ignored. */
1817
/* -------------------------------------------------------------------------- */
1918
lwipClient::lwipClient(uint8_t sock)
20-
: _tcp_client(NULL)
2119
{
2220
}
2321
/* -------------------------------------------------------------------------- */
2422

2523
/* -------------------------------------------------------------------------- */
2624
lwipClient::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

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)