From bb8a4be517569abd5ad74ca3e824f26781137e33 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 3 Oct 2025 22:22:12 -0700 Subject: [PATCH] Properly clean up on SimpleMDNS::end Cleans up a couple memory leaks on a (rare/never seen?) SimpleMDNS::end() and avoid pulling in std::string. --- libraries/SimpleMDNS/src/SimpleMDNS.cpp | 12 ++++++++---- libraries/SimpleMDNS/src/SimpleMDNS.h | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libraries/SimpleMDNS/src/SimpleMDNS.cpp b/libraries/SimpleMDNS/src/SimpleMDNS.cpp index 48e5ed7a0..2921dbdad 100644 --- a/libraries/SimpleMDNS/src/SimpleMDNS.cpp +++ b/libraries/SimpleMDNS/src/SimpleMDNS.cpp @@ -83,7 +83,7 @@ void SimpleMDNS::enableArduino(unsigned int port, bool passwd) { return; } SimpleMDNSService *svc = new SimpleMDNSService(); - svc->_service = "arduino"; + svc->_service = strdup("arduino"); svc->_proto = DNSSD_PROTO_TCP; svc->_port = port; svc->_fn = _arduinoGetTxt; @@ -140,7 +140,13 @@ void SimpleMDNS::end() { __setStateChangeCallback(nullptr); __setAddNetifCallback(nullptr); __setRemoveNetifCallback(nullptr); - + for (auto svc : _svcMap) { + free(svc.second->_service); + delete svc.second; + } + _svcMap.clear(); + free(_hostname); + _arduinoAdded = false; } _running = false; } @@ -219,6 +225,4 @@ hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_ } -const char *SimpleMDNS::_hostname = nullptr; - SimpleMDNS MDNS; diff --git a/libraries/SimpleMDNS/src/SimpleMDNS.h b/libraries/SimpleMDNS/src/SimpleMDNS.h index e02a4ddea..c493abd62 100644 --- a/libraries/SimpleMDNS/src/SimpleMDNS.h +++ b/libraries/SimpleMDNS/src/SimpleMDNS.h @@ -34,7 +34,7 @@ class SimpleMDNSService { static void callback(struct mdns_service *service, void *txt_userdata); hMDNSTxt add(const char *key, const char *val); - const char *_service; + char *_service; uint16_t _proto; uint16_t _port; service_get_txt_fn_t _fn; @@ -80,8 +80,8 @@ class SimpleMDNS { bool _running = false; bool _lwipMSNDInitted = false; - static const char *_hostname; - std::map _svcMap; + char *_hostname; + std::map _svcMap; bool _arduinoAdded = false; // LwipIntfDev helpers when netifs come up and down, to ensure we set the old services on the new netif