@@ -172,7 +172,6 @@ static void _getHostByNameCBK(const char *name, const ip_addr_t *ipaddr, void *c
172172
173173 cbk->cbk (toArduinoIP (ipaddr));
174174
175- delete ipaddr;
176175 delete cbk;
177176}
178177
@@ -240,18 +239,23 @@ int CLwipIf::getHostByName(const char* aHostname, IPAddress& aResult, bool execu
240239
241240// TODO instead of returning int return an enum value
242241int CLwipIf::getHostByName (const char * aHostname, std::function<void (const IPAddress&)> cbk) {
243- ip_addr_t *addr = new ip_addr_t ;
242+ /*
243+ * according to lwip documentation: addr is a pointer to a ip_addr_t where to store the address if it is already cached
244+ * in the dns_table (only valid if ERR_OK is returned!); thus this won't be the same ip_addr_t passed to the callback,
245+ * there is no need to allocate it in the heap and delete it afterwards.
246+ * on the contrary the struct dns_cbk must be allocated in the heap
247+ */
248+ ip_addr_t addr;
244249 uint8_t res = 0 ;
245250
246251 dns_callback* dns_cbk = new dns_callback;
247252 dns_cbk->cbk = cbk;
248- err_t err = dns_gethostbyname (aHostname, addr, _getHostByNameCBK, dns_cbk);
253+ err_t err = dns_gethostbyname (aHostname, & addr, _getHostByNameCBK, dns_cbk);
249254
250255 switch (err) {
251256 case ERR_OK:
252257 // the address was already present in the local cache
253- cbk (toArduinoIP (addr));
254- delete addr;
258+ cbk (toArduinoIP (&addr));
255259 delete dns_cbk;
256260 break ;
257261 case ERR_INPROGRESS:
@@ -260,7 +264,6 @@ int CLwipIf::getHostByName(const char* aHostname, std::function<void(const IPAdd
260264 break ;
261265 case ERR_ARG: // there are issues in the arguments passed
262266 default :
263- delete addr;
264267 delete dns_cbk;
265268 res = -1 ;
266269 }
0 commit comments