From: Antoine Naud Date: Thu, 22 Mar 2018 08:31:23 +0000 (+0000) Subject: src/virt_test.c: Fix memory leaks detected by valgrind X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f8e92a1940f3662f144e5f362521725d0f4d8ec1;p=collectd.git src/virt_test.c: Fix memory leaks detected by valgrind src/virt.c: The same memory leaks are also fixed. Change-Id: Ie970ec5ce500be7cc06512a52994b37cfb687e84 Signed-off-by: Antoine Naud --- diff --git a/src/virt.c b/src/virt.c index f39f9c26..28991779 100644 --- a/src/virt.c +++ b/src/virt.c @@ -1890,7 +1890,7 @@ static int persistent_domains_state_notification(void) { int status = 0; int n; #ifdef HAVE_LIST_ALL_DOMAINS - virDomainPtr *domains; + virDomainPtr *domains = NULL; n = virConnectListAllDomains(conn, &domains, VIR_CONNECT_LIST_DOMAINS_PERSISTENT); if (n < 0) { @@ -1907,6 +1907,7 @@ static int persistent_domains_state_notification(void) { ERROR(PLUGIN_NAME " plugin: could not notify state of domain %s", virDomainGetName(domains[i])); } + virDomainFree(domains[i]); } sfree(domains); @@ -2256,6 +2257,8 @@ static int refresh_lists(struct lv_read_instance *inst) { #ifndef HAVE_LIST_ALL_DOMAINS sfree(domids); #else + for (int i = 0; i < m; ++i) + virDomainFree(domains_inactive[i]); sfree(domains_inactive); #endif return -1; @@ -2432,7 +2435,11 @@ static int refresh_lists(struct lv_read_instance *inst) { } #ifdef HAVE_LIST_ALL_DOMAINS + for (int i = 0; i < n; ++i) + virDomainFree(domains[i]); sfree(domains); + for (int i = 0; i < m; ++i) + virDomainFree(domains_inactive[i]); sfree(domains_inactive); #else sfree(domids); diff --git a/src/virt_test.c b/src/virt_test.c index 4596e5df..b21ac306 100644 --- a/src/virt_test.c +++ b/src/virt_test.c @@ -25,7 +25,8 @@ #include "testing.h" #include "virt.c" /* sic */ -virDomainPtr *domains; +static virDomainPtr *domains = NULL; +static int nr_domains = 0; static int setup(void) { if (virInitialize() != 0) { @@ -43,7 +44,12 @@ static int setup(void) { } static int teardown(void) { - sfree(domains); + if (domains) { + for (int i = 0; i < nr_domains; ++i) + virDomainFree(domains[i]); + sfree(domains); + } + nr_domains = 0; if (conn != NULL) virConnectClose(conn); @@ -53,10 +59,10 @@ static int teardown(void) { #ifdef HAVE_LIST_ALL_DOMAINS DEF_TEST(get_domain_state_notify) { if (setup() == 0) { - int n_domains = virConnectListAllDomains( + nr_domains = virConnectListAllDomains( conn, &domains, VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT); - if (n_domains <= 0) { - printf("ERROR: virConnectListAllDomains: n_domains <= 0\n"); + if (nr_domains <= 0) { + printf("ERROR: virConnectListAllDomains: nr_domains <= 0\n"); return -1; }