From ca0cb7b0821f4ffb43374356f55546299becab7c Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Mon, 7 Oct 2024 01:02:39 +0900 Subject: [PATCH] fixed some memleaks --- src/app/backend/pcmanager/discovery/impl/microdns.c | 8 ++++---- src/app/backend/pcmanager/discovery/throttle.c | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/app/backend/pcmanager/discovery/impl/microdns.c b/src/app/backend/pcmanager/discovery/impl/microdns.c index ae6fe017..fbae12d9 100644 --- a/src/app/backend/pcmanager/discovery/impl/microdns.c +++ b/src/app/backend/pcmanager/discovery/impl/microdns.c @@ -66,7 +66,7 @@ void discovery_callback(discovery_task_t *task, int status, const struct rr_entr return; } if (task->stop) { return; } - struct sockaddr *addr = sockaddr_new(); + sockaddr_t *addr = sockaddr_new(); for (const struct rr_entry *cur = entries; cur != NULL; cur = cur->next) { switch (cur->type) { case RR_A: { @@ -87,10 +87,10 @@ void discovery_callback(discovery_task_t *task, int status, const struct rr_entr } } } - if (addr->sa_family == AF_UNSPEC) { - return; + if (addr->sa_family != AF_UNSPEC) { + discovery_discovered(task->discovery, addr); } - discovery_discovered(task->discovery, addr); + sockaddr_free(addr); } bool discovery_is_stopped(discovery_task_t *task) { diff --git a/src/app/backend/pcmanager/discovery/throttle.c b/src/app/backend/pcmanager/discovery/throttle.c index 93563641..a87ad6d3 100644 --- a/src/app/backend/pcmanager/discovery/throttle.c +++ b/src/app/backend/pcmanager/discovery/throttle.c @@ -47,6 +47,8 @@ static int throttle_hosts_find_not_expired(discovery_throttle_host_t *node, cons static void throttle_hosts_evict(discovery_throttle_host_t **head); +static void throttle_host_free(discovery_throttle_host_t *node); + void discovery_throttle_init(discovery_throttle_t *throttle, discovery_callback callback, void *user_data) { throttle->callback = callback; throttle->user_data = user_data; @@ -56,7 +58,7 @@ void discovery_throttle_init(discovery_throttle_t *throttle, discovery_callback void discovery_throttle_deinit(discovery_throttle_t *throttle) { SDL_LockMutex(throttle->lock); - throttle_hosts_free(throttle->hosts, NULL); + throttle_hosts_free(throttle->hosts, throttle_host_free); SDL_UnlockMutex(throttle->lock); SDL_DestroyMutex(throttle->lock); } @@ -82,7 +84,7 @@ void discovery_throttle_on_discovered(discovery_throttle_t *throttle, const sock throttle->hosts = throttle_hosts_sortedinsert(throttle->hosts, node, throttle_hosts_compare_time); if (throttle->callback != NULL) { - throttle->callback(addr, throttle->user_data); + throttle->callback(node->addr, throttle->user_data); } SDL_UnlockMutex(throttle->lock); } @@ -112,3 +114,8 @@ void throttle_hosts_evict(discovery_throttle_host_t **head) { int throttle_hosts_find_not_expired(discovery_throttle_host_t *node, const void *now) { return SDL_TICKS_PASSED(*(Uint32 *) now, node->last_discovered + node->ttl); } + +void throttle_host_free(discovery_throttle_host_t *node) { + sockaddr_free(node->addr); + free(node); +}