From ebe2184e2a4f277baabcf1d3cde6619a5b3c5d46 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Wed, 13 Dec 2023 22:00:01 +0100 Subject: [PATCH] lwipClient - fix mem pool leak with shared_ptr --- libraries/lwIpWrapper/src/lwipClient.cpp | 14 ++++++++------ libraries/lwIpWrapper/src/lwipClient.h | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libraries/lwIpWrapper/src/lwipClient.cpp b/libraries/lwIpWrapper/src/lwipClient.cpp index 8facb252..4109306a 100644 --- a/libraries/lwIpWrapper/src/lwipClient.cpp +++ b/libraries/lwIpWrapper/src/lwipClient.cpp @@ -8,7 +8,6 @@ extern "C" { /* -------------------------------------------------------------------------- */ lwipClient::lwipClient() - : _tcp_client(NULL) { } /* -------------------------------------------------------------------------- */ @@ -17,7 +16,6 @@ lwipClient::lwipClient() sketches but sock is ignored. */ /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(uint8_t sock) - : _tcp_client(NULL) { } /* -------------------------------------------------------------------------- */ @@ -25,7 +23,10 @@ lwipClient::lwipClient(uint8_t sock) /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(struct tcp_struct* tcpClient) { - _tcp_client = tcpClient; + if (tcpClient == NULL) + return; + _tcp_client.reset(tcpClient, + [](struct tcp_struct *tcp_client) { (void) tcp_client; } ); // empty deleter } /* -------------------------------------------------------------------------- */ @@ -49,7 +50,8 @@ int lwipClient::connect(IPAddress ip, uint16_t port) /* -------------------------------------------------------------------------- */ if (_tcp_client == NULL) { /* Allocates memory for client */ - _tcp_client = (struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)); + _tcp_client.reset((struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)), + [](struct tcp_struct *tcp_client) { mem_free(tcp_client); } ); if (_tcp_client == NULL) { return 0; @@ -69,7 +71,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port) uint32_t startTime = millis(); ip_addr_t ipaddr; - tcp_arg(_tcp_client->pcb, _tcp_client); + tcp_arg(_tcp_client->pcb, _tcp_client.get()); if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { stop(); return 0; @@ -215,7 +217,7 @@ void lwipClient::stop() // close tcp connection if not closed yet if (status() != TCP_CLOSING) { - tcp_connection_close(_tcp_client->pcb, _tcp_client); + tcp_connection_close(_tcp_client->pcb, _tcp_client.get()); } } diff --git a/libraries/lwIpWrapper/src/lwipClient.h b/libraries/lwIpWrapper/src/lwipClient.h index 511b3c7f..0dc282d3 100644 --- a/libraries/lwIpWrapper/src/lwipClient.h +++ b/libraries/lwIpWrapper/src/lwipClient.h @@ -9,6 +9,7 @@ #include "lwipMem.h" #include "lwipTcp.h" #include "lwipTypes.h" +#include class lwipClient : public Client { @@ -66,7 +67,7 @@ class lwipClient : public Client { using Print::write; private: - struct tcp_struct* _tcp_client; + std::shared_ptr _tcp_client; uint16_t _timeout = 10000; };