Skip to content

Commit

Permalink
By using a smart pointer (std::shared_ptr<T>) we can eliminate all he…
Browse files Browse the repository at this point in the history
…adache concerning memory leaks.
  • Loading branch information
aentinger committed Oct 15, 2024
1 parent d2bb37b commit 738944e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/Arduino_10BASE_T1S_UDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ void Arduino_10BASE_T1S_UDP::flush()
{
/* Drop packet from receive buffer. */
if (_rx_pkt_list.size())
{
auto const rx_pkt = _rx_pkt_list.front();
_rx_pkt_list.pop_front();
delete rx_pkt;
}
}

IPAddress Arduino_10BASE_T1S_UDP::remoteIP()
Expand Down Expand Up @@ -220,7 +216,7 @@ void Arduino_10BASE_T1S_UDP::onUdpRawRecv(struct udp_pcb *pcb, struct pbuf *p, c
auto const remote_port = port;

/* Create UDP object. */
auto const rx_pkt = new UdpRxPacket(
auto const rx_pkt = std::make_shared<UdpRxPacket>(
remote_ip,
remote_port,
(uint8_t *)p->payload,
Expand Down
5 changes: 4 additions & 1 deletion src/Arduino_10BASE_T1S_UDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <list>
#include <deque>
#include <vector>
#include <memory>

#include <api/Udp.h>
#include <api/IPAddress.h>
Expand Down Expand Up @@ -98,6 +99,8 @@ class Arduino_10BASE_T1S_UDP : public UDP
std::copy(p_data, p_data + data_len, std::back_inserter(_rx_data));
}

typedef std::shared_ptr<UdpRxPacket> SharedPtr;

IPAddress remoteIP() const { return _remote_ip; }
uint16_t remotePort() const { return _remote_port; }
size_t totalSize() const { return _rx_data_len; }
Expand Down Expand Up @@ -135,5 +138,5 @@ class Arduino_10BASE_T1S_UDP : public UDP
return _rx_data.front();
}
};
std::list<UdpRxPacket *> _rx_pkt_list;
std::list<UdpRxPacket::SharedPtr> _rx_pkt_list;
};

0 comments on commit 738944e

Please sign in to comment.