From 518fcd38834ef2f7b19707ccaa52914c2bea9f91 Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Sun, 8 Jan 2017 20:19:29 +0100 Subject: [PATCH] target: Fix memory leak MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ib23dfd653d8edacb890a46179e9d437c027d58e8 Signed-off-by: Marc Schink Reviewed-on: http://openocd.zylin.com/4048 Tested-by: jenkins Reviewed-by: Tomas Vanek Reviewed-by: Chengyu Zheng Reviewed-by: Andreas Färber --- src/target/target.c | 23 +++++++++++++++++++---- src/target/target.h | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/target/target.c b/src/target/target.c index ee302ee4..e04ecc47 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -1870,6 +1870,17 @@ int target_free_working_area(struct target *target, struct working_area *area) return target_free_working_area_restore(target, area, 1); } +static void target_destroy(struct target *target) +{ + if (target->type->deinit_target) + target->type->deinit_target(target); + + free(target->type); + free(target->trace_info); + free(target->cmd_name); + free(target); +} + void target_quit(void) { struct target_event_callback *pe = target_event_callbacks; @@ -1888,11 +1899,15 @@ void target_quit(void) } target_timer_callbacks = NULL; - for (struct target *target = all_targets; - target; target = target->next) { - if (target->type->deinit_target) - target->type->deinit_target(target); + for (struct target *target = all_targets; target;) { + struct target *tmp; + + tmp = target->next; + target_destroy(target); + target = tmp; } + + all_targets = NULL; } /* free resources and restore memory, if restoring memory fails, diff --git a/src/target/target.h b/src/target/target.h index 76630b97..53f9e261 100644 --- a/src/target/target.h +++ b/src/target/target.h @@ -125,7 +125,7 @@ enum target_register_class { /* target_type.h contains the full definition of struct target_type */ struct target { struct target_type *type; /* target type definition (name, access functions) */ - const char *cmd_name; /* tcl Name of target */ + char *cmd_name; /* tcl Name of target */ int target_number; /* DO NOT USE! field to be removed in 2010 */ struct jtag_tap *tap; /* where on the jtag chain is this */ int32_t coreid; /* which device on the TAP? */ -- 2.39.5