Skip to content

Commit 738944e

Browse files
committed
By using a smart pointer (std::shared_ptr<T>) we can eliminate all headache concerning memory leaks.
1 parent d2bb37b commit 738944e

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/Arduino_10BASE_T1S_UDP.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ void Arduino_10BASE_T1S_UDP::flush()
186186
{
187187
/* Drop packet from receive buffer. */
188188
if (_rx_pkt_list.size())
189-
{
190-
auto const rx_pkt = _rx_pkt_list.front();
191189
_rx_pkt_list.pop_front();
192-
delete rx_pkt;
193-
}
194190
}
195191

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

222218
/* Create UDP object. */
223-
auto const rx_pkt = new UdpRxPacket(
219+
auto const rx_pkt = std::make_shared<UdpRxPacket>(
224220
remote_ip,
225221
remote_port,
226222
(uint8_t *)p->payload,

src/Arduino_10BASE_T1S_UDP.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <list>
1818
#include <deque>
1919
#include <vector>
20+
#include <memory>
2021

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

102+
typedef std::shared_ptr<UdpRxPacket> SharedPtr;
103+
101104
IPAddress remoteIP() const { return _remote_ip; }
102105
uint16_t remotePort() const { return _remote_port; }
103106
size_t totalSize() const { return _rx_data_len; }
@@ -135,5 +138,5 @@ class Arduino_10BASE_T1S_UDP : public UDP
135138
return _rx_data.front();
136139
}
137140
};
138-
std::list<UdpRxPacket *> _rx_pkt_list;
141+
std::list<UdpRxPacket::SharedPtr> _rx_pkt_list;
139142
};

0 commit comments

Comments
 (0)