about summary refs log tree commit homepage
path: root/mwrap_core.h
diff options
context:
space:
mode:
Diffstat (limited to 'mwrap_core.h')
-rw-r--r--mwrap_core.h14
1 files changed, 10 insertions, 4 deletions
diff --git a/mwrap_core.h b/mwrap_core.h
index ec08ee1..2ef550a 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -714,6 +714,14 @@ char **bt_syms(void * const *addrlist, uint32_t size)
         return s;
 }
 
+/* supported by modern gcc + clang */
+#define AUTO_FREE __attribute__((__cleanup__(cleanup_free)))
+static void cleanup_free(void *any)
+{
+        void **p = any;
+        free(*p);
+}
+
 static void *dump_to_file(struct dump_arg *a)
 {
         struct cds_lfht_iter iter;
@@ -730,13 +738,11 @@ static void *dump_to_file(struct dump_arg *a)
                 if (l->total <= a->min) continue;
 
                 if (loc_is_addr(l)) {
-                        char **s = bt_syms(l->bt, 1);
+                        AUTO_FREE char **s = bt_syms(l->bt, 1);
 
-                        if (s) {
+                        if (s)
                                 fprintf(a->fp, "%16zu %12zu %s\n",
                                         l->total, l->allocations, s[0]);
-                                free(s);
-                        }
                 } else {
                         fprintf(a->fp, "%16zu %12zu %s:%zu\n",
                                 l->total, l->allocations, l->f->fn, l->lineno);