All of lore.kernel.org
 help / color / mirror / Atom feed
From: "?ukasz Majewski" <lukma@denx.de>
To: yocto@lists.yoctoproject.org
Cc: Adrian Freihofer <adrian.freihofer@siemens.com>
Subject: [ptest-runner 2/5] mem: Fix memleak for ptest_opts
Date: Wed, 21 Jul 2021 11:46:24 +0200	[thread overview]
Message-ID: <20210721094627.29833-3-lukma@denx.de> (raw)
In-Reply-To: <20210721094627.29833-1-lukma@denx.de>

From: Adrian Freihofer <adrian.freihofer@siemens.com>

make && valgrind -s --leak-check=full ./ptest-runner -d tests/data2

==4154029== HEAP SUMMARY:
==4154029==     in use at exit: 20 bytes in 2 blocks
==4154029==   total heap usage: 45 allocs, 43 frees, 42,909 bytes allocated
==4154029==
==4154029== 20 (8 direct, 12 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==4154029==    at 0x4839809: malloc (vg_replace_malloc.c:307)
==4154029==    by 0x40252D: str2array (main.c:70)
==4154029==    by 0x402768: main (main.c:119)
==4154029==
==4154029== LEAK SUMMARY:
==4154029==    definitely lost: 8 bytes in 1 blocks
==4154029==    indirectly lost: 12 bytes in 1 blocks
==4154029==      possibly lost: 0 bytes in 0 blocks
==4154029==    still reachable: 0 bytes in 0 blocks
==4154029==         suppressed: 0 bytes in 0 blocks
==4154029==
==4154029== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

With this patch valgrind reports 0 errors.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 main.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/main.c b/main.c
index 467548e..e73626c 100644
--- a/main.c
+++ b/main.c
@@ -84,6 +84,25 @@ str2array(char *str, const char *delim, int *num)
 	return array;
 }
 
+void cleanup_ptest_opts(struct ptest_options *opts)
+{
+	for (int i=0; i < opts->dirs_no; i++)
+		free(opts->dirs[i]);
+
+	free(opts->dirs);
+	opts->dirs = NULL;
+
+	if (opts->ptests) {
+		free(opts->ptests);
+		opts->ptests = NULL;
+	}
+
+	if (opts->xml_filename) {
+		free(opts->xml_filename);
+		opts->xml_filename = NULL;
+	}
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -98,7 +117,7 @@ main(int argc, char *argv[])
 #endif
 
 	struct ptest_list *head, *run;
-	struct ptest_options opts;
+	__attribute__ ((__cleanup__(cleanup_ptest_opts))) struct ptest_options opts;
 
 	opts.dirs = malloc(sizeof(char **) * 1);
 	CHECK_ALLOCATION(opts.dirs, 1, 1);
-- 
2.20.1


  parent reply	other threads:[~2021-07-21  9:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-21  9:46 [ptest-runner 0/5] ptest: Various memory fixes and enhancements ?ukasz Majewski
2021-07-21  9:46 ` [ptest-runner 1/5] git: Extend the gitignore ?ukasz Majewski
2021-07-26 15:44   ` [yocto] " Anibal Limon
2021-07-21  9:46 ` ?ukasz Majewski [this message]
2021-07-26 16:02   ` [yocto] [ptest-runner 2/5] mem: Fix memleak for ptest_opts Anibal Limon
2021-07-21  9:46 ` [ptest-runner 3/5] mem: Simplify memory management ?ukasz Majewski
2021-07-26 16:05   ` [yocto] " Anibal Limon
2021-07-21  9:46 ` [ptest-runner 4/5] mem: Refactor ptest_list cleanup ?ukasz Majewski
2021-07-26 16:10   ` [yocto] " Anibal Limon
2021-07-26 16:25     ` Anibal Limon
2021-07-21  9:46 ` [ptest-runner 5/5] main: Do not return number of failed tests when calling ptest-runner ?ukasz Majewski
2021-07-26  7:00   ` Tero Kinnunen
2021-07-26 15:40     ` [yocto] " Anibal Limon
2021-07-27 15:54   ` Adrian Freihofer
2021-07-21 23:36 ` [yocto] [ptest-runner 0/5] ptest: Various memory fixes and enhancements Randy MacLeod
2021-07-22  9:27   ` ?ukasz Majewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210721094627.29833-3-lukma@denx.de \
    --to=lukma@denx.de \
    --cc=adrian.freihofer@siemens.com \
    --cc=yocto@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.