From b859fcd0fab43ae1013c4da3b31fc3808aeb4164 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 26 Sep 2023 15:18:54 +0200 Subject: [PATCH] Provided a destructor for lwipClient Provided a destructor that takes care of deallocating the _tcp_client only when allocated with mem_alloc() and not passed in the constructor --- libraries/lwIpWrapper/src/lwipClient.cpp | 19 ++++++++++++++++--- libraries/lwIpWrapper/src/lwipClient.h | 3 +++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libraries/lwIpWrapper/src/lwipClient.cpp b/libraries/lwIpWrapper/src/lwipClient.cpp index 8facb252..927df373 100644 --- a/libraries/lwIpWrapper/src/lwipClient.cpp +++ b/libraries/lwIpWrapper/src/lwipClient.cpp @@ -8,7 +8,7 @@ extern "C" { /* -------------------------------------------------------------------------- */ lwipClient::lwipClient() - : _tcp_client(NULL) + : _tcp_client(NULL), _provided_tcp_client(false) { } /* -------------------------------------------------------------------------- */ @@ -17,15 +17,28 @@ lwipClient::lwipClient() sketches but sock is ignored. */ /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(uint8_t sock) - : _tcp_client(NULL) + : _tcp_client(NULL), _provided_tcp_client(false) + { } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(struct tcp_struct* tcpClient) + : _tcp_client(tcpClient), _provided_tcp_client(true) + { - _tcp_client = tcpClient; +} +/* -------------------------------------------------------------------------- */ + +/* -------------------------------------------------------------------------- */ +lwipClient::~lwipClient() +{ + stop(); + + if(!_provided_tcp_client) { + mem_free(_tcp_client); + } } /* -------------------------------------------------------------------------- */ diff --git a/libraries/lwIpWrapper/src/lwipClient.h b/libraries/lwIpWrapper/src/lwipClient.h index b3ffe270..f71fcb09 100644 --- a/libraries/lwIpWrapper/src/lwipClient.h +++ b/libraries/lwIpWrapper/src/lwipClient.h @@ -16,6 +16,7 @@ class lwipClient : public Client { lwipClient(); lwipClient(uint8_t sock); lwipClient(struct tcp_struct* tcpClient); + virtual ~lwipClient(); uint8_t status(); virtual int connect(IPAddress ip, uint16_t port); @@ -68,6 +69,8 @@ class lwipClient : public Client { private: struct tcp_struct* _tcp_client; uint16_t _timeout = 10000; + + const bool _provided_tcp_client; }; #endif