udp_multicast example: Fix memory leak from getaddrinfo() calls

Closes https://github.com/espressif/esp-idf/issues/2201
This commit is contained in:
Angus Gratton 2018-07-23 16:15:02 +10:00 committed by Angus Gratton
parent a7d00f44ab
commit 5fb665dbaf

View file

@ -506,6 +506,7 @@ static void mcast_example_task(void *pvParameters)
ESP_LOGI(TAG, "Sending to IPV6 (V4 mapped) multicast address %s (%s)...", addrbuf, CONFIG_EXAMPLE_MULTICAST_IPV4_ADDR); ESP_LOGI(TAG, "Sending to IPV6 (V4 mapped) multicast address %s (%s)...", addrbuf, CONFIG_EXAMPLE_MULTICAST_IPV4_ADDR);
#endif #endif
err = sendto(sock, sendbuf, len, 0, res->ai_addr, res->ai_addrlen); err = sendto(sock, sendbuf, len, 0, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (err < 0) { if (err < 0) {
ESP_LOGE(TAG, "IPV4 sendto failed. errno: %d", errno); ESP_LOGE(TAG, "IPV4 sendto failed. errno: %d", errno);
break; break;
@ -523,12 +524,12 @@ static void mcast_example_task(void *pvParameters)
break; break;
} }
struct sockaddr_in6 *s6addr = (struct sockaddr_in6 *)res->ai_addr; struct sockaddr_in6 *s6addr = (struct sockaddr_in6 *)res->ai_addr;
s6addr->sin6_port = htons(UDP_PORT); s6addr->sin6_port = htons(UDP_PORT);
inet6_ntoa_r(s6addr->sin6_addr, addrbuf, sizeof(addrbuf)-1); inet6_ntoa_r(s6addr->sin6_addr, addrbuf, sizeof(addrbuf)-1);
ESP_LOGI(TAG, "Sending to IPV6 multicast address %s...", addrbuf); ESP_LOGI(TAG, "Sending to IPV6 multicast address %s...", addrbuf);
err = sendto(sock, sendbuf, len, 0, res->ai_addr, res->ai_addrlen); err = sendto(sock, sendbuf, len, 0, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (err < 0) { if (err < 0) {
ESP_LOGE(TAG, "IPV6 sendto failed. errno: %d", errno); ESP_LOGE(TAG, "IPV6 sendto failed. errno: %d", errno);
break; break;