@@ -172,7 +172,6 @@ static void _getHostByNameCBK(const char *name, const ip_addr_t *ipaddr, void *c
172
172
173
173
cbk->cbk (toArduinoIP (ipaddr));
174
174
175
- delete ipaddr;
176
175
delete cbk;
177
176
}
178
177
@@ -240,18 +239,23 @@ int CLwipIf::getHostByName(const char* aHostname, IPAddress& aResult, bool execu
240
239
241
240
// TODO instead of returning int return an enum value
242
241
int 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;
244
249
uint8_t res = 0 ;
245
250
246
251
dns_callback* dns_cbk = new dns_callback;
247
252
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);
249
254
250
255
switch (err) {
251
256
case ERR_OK:
252
257
// the address was already present in the local cache
253
- cbk (toArduinoIP (addr));
254
- delete addr;
258
+ cbk (toArduinoIP (&addr));
255
259
delete dns_cbk;
256
260
break ;
257
261
case ERR_INPROGRESS:
@@ -260,7 +264,6 @@ int CLwipIf::getHostByName(const char* aHostname, std::function<void(const IPAdd
260
264
break ;
261
265
case ERR_ARG: // there are issues in the arguments passed
262
266
default :
263
- delete addr;
264
267
delete dns_cbk;
265
268
res = -1 ;
266
269
}
0 commit comments