Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lwipClient - fix mem pool leak #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading