Skip to content

Commit

Permalink
lwipClient - fix mem pool leak with shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Dec 15, 2023
1 parent 795a81d commit ebe2184
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions libraries/lwIpWrapper/src/lwipClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ extern "C" {

/* -------------------------------------------------------------------------- */
lwipClient::lwipClient()
: _tcp_client(NULL)
{
}
/* -------------------------------------------------------------------------- */
Expand All @@ -17,15 +16,17 @@ lwipClient::lwipClient()
sketches but sock is ignored. */
/* -------------------------------------------------------------------------- */
lwipClient::lwipClient(uint8_t sock)
: _tcp_client(NULL)
{
}
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
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
}
/* -------------------------------------------------------------------------- */

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down
3 changes: 2 additions & 1 deletion libraries/lwIpWrapper/src/lwipClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "lwipMem.h"
#include "lwipTcp.h"
#include "lwipTypes.h"
#include <memory>

class lwipClient : public Client {

Expand Down Expand Up @@ -66,7 +67,7 @@ class lwipClient : public Client {
using Print::write;

private:
struct tcp_struct* _tcp_client;
std::shared_ptr<struct tcp_struct> _tcp_client;
uint16_t _timeout = 10000;
};

Expand Down

0 comments on commit ebe2184

Please sign in to comment.