Skip to content

Commit bc0093d

Browse files
Properly clean up on SimpleMDNS::end (#3186)
Cleans up a couple memory leaks on a (rare/never seen?) SimpleMDNS::end() and avoid pulling in std::string.
1 parent b35aaa4 commit bc0093d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

libraries/SimpleMDNS/src/SimpleMDNS.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void SimpleMDNS::enableArduino(unsigned int port, bool passwd) {
8383
return;
8484
}
8585
SimpleMDNSService *svc = new SimpleMDNSService();
86-
svc->_service = "arduino";
86+
svc->_service = strdup("arduino");
8787
svc->_proto = DNSSD_PROTO_TCP;
8888
svc->_port = port;
8989
svc->_fn = _arduinoGetTxt;
@@ -140,7 +140,13 @@ void SimpleMDNS::end() {
140140
__setStateChangeCallback(nullptr);
141141
__setAddNetifCallback(nullptr);
142142
__setRemoveNetifCallback(nullptr);
143-
143+
for (auto svc : _svcMap) {
144+
free(svc.second->_service);
145+
delete svc.second;
146+
}
147+
_svcMap.clear();
148+
free(_hostname);
149+
_arduinoAdded = false;
144150
}
145151
_running = false;
146152
}
@@ -219,6 +225,4 @@ hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_
219225
}
220226

221227

222-
const char *SimpleMDNS::_hostname = nullptr;
223-
224228
SimpleMDNS MDNS;

libraries/SimpleMDNS/src/SimpleMDNS.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SimpleMDNSService {
3434
static void callback(struct mdns_service *service, void *txt_userdata);
3535
hMDNSTxt add(const char *key, const char *val);
3636

37-
const char *_service;
37+
char *_service;
3838
uint16_t _proto;
3939
uint16_t _port;
4040
service_get_txt_fn_t _fn;
@@ -80,8 +80,8 @@ class SimpleMDNS {
8080

8181
bool _running = false;
8282
bool _lwipMSNDInitted = false;
83-
static const char *_hostname;
84-
std::map<std::string, SimpleMDNSService*> _svcMap;
83+
char *_hostname;
84+
std::map<const char *, SimpleMDNSService*> _svcMap;
8585
bool _arduinoAdded = false;
8686

8787
// LwipIntfDev helpers when netifs come up and down, to ensure we set the old services on the new netif

0 commit comments

Comments
 (0)