2222#include < arpa/inet.h> // for inet_pton
2323#include < netdb.h> // for gethostbyname, struct hostent
2424#include < unistd.h> // for close
25+ #include < memory> // This is the include you need
2526
2627#include " ArduinoLogger.h"
2728#include " RingBufferExt.h"
@@ -75,13 +76,13 @@ class EthernetClient : public Client {
7576 public:
7677 EthernetClient () {
7778 setTimeout (2000 );
78- p_sock = new SocketImpl ();
79+ p_sock = std::make_shared< SocketImpl> ();
7980 readBuffer = RingBufferExt (bufferSize);
8081 writeBuffer = RingBufferExt (bufferSize);
8182 registerCleanup ();
8283 active_clients ().push_back (this );
8384 }
84- EthernetClient (SocketImpl* sock, int bufferSize = 256 , long timeout = 2000 ) {
85+ EthernetClient (std::shared_ptr< SocketImpl> sock, int bufferSize = 256 , long timeout = 2000 ) {
8586 if (sock) {
8687 setTimeout (timeout);
8788 this ->bufferSize = bufferSize;
@@ -97,22 +98,8 @@ class EthernetClient : public Client {
9798 setTimeout (2000 );
9899 readBuffer = RingBufferExt (bufferSize);
99100 writeBuffer = RingBufferExt (bufferSize);
100- p_sock = new SocketImpl (socket);
101+ p_sock = std::make_shared< SocketImpl> (socket);
101102 is_connected = p_sock->connected ();
102- registerCleanup ();
103- active_clients ().push_back (this );
104- }
105-
106- virtual ~EthernetClient () {
107- auto & clients = active_clients ();
108- auto it = std::find (clients.begin (), clients.end (), this );
109- if (it != clients.end ()) {
110- clients.erase (it);
111- }
112- if (p_sock) {
113- delete p_sock;
114- p_sock = nullptr ;
115- }
116103 }
117104
118105 // checks if we are connected - using a timeout
@@ -264,7 +251,7 @@ class EthernetClient : public Client {
264251
265252 protected:
266253 const char * WIFICLIENT = " EthernetClient" ;
267- SocketImpl* p_sock = nullptr ;
254+ std::shared_ptr< SocketImpl> p_sock = nullptr ;
268255 int bufferSize = 256 ;
269256 RingBufferExt readBuffer;
270257 RingBufferExt writeBuffer;
0 commit comments