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