Skip to content

Commit

Permalink
Provided a destructor for lwipClient
Browse files Browse the repository at this point in the history
Provided a destructor that takes care of deallocating the _tcp_client
only when allocated with mem_alloc() and not passed in the constructor
  • Loading branch information
andreagilardoni committed Sep 28, 2023
1 parent 4896ed2 commit b859fcd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 16 additions & 3 deletions libraries/lwIpWrapper/src/lwipClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {

/* -------------------------------------------------------------------------- */
lwipClient::lwipClient()
: _tcp_client(NULL)
: _tcp_client(NULL), _provided_tcp_client(false)
{
}
/* -------------------------------------------------------------------------- */
Expand All @@ -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);
}
}
/* -------------------------------------------------------------------------- */

Expand Down
3 changes: 3 additions & 0 deletions libraries/lwIpWrapper/src/lwipClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -68,6 +69,8 @@ class lwipClient : public Client {
private:
struct tcp_struct* _tcp_client;
uint16_t _timeout = 10000;

const bool _provided_tcp_client;
};

#endif

0 comments on commit b859fcd

Please sign in to comment.