target: Fix memory leak 48/4048/2
authorMarc Schink <openocd-dev@marcschink.de>
Sun, 8 Jan 2017 19:19:29 +0000 (20:19 +0100)
committerFreddie Chopin <freddie.chopin@gmail.com>
Sun, 23 Apr 2017 19:48:45 +0000 (20:48 +0100)
Change-Id: Ib23dfd653d8edacb890a46179e9d437c027d58e8
Signed-off-by: Marc Schink <openocd-dev@marcschink.de>
Reviewed-on: http://openocd.zylin.com/4048
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-by: Chengyu Zheng <chengyu.zheng@polimi.it>
Reviewed-by: Andreas Färber <afaerber@suse.de>
src/target/target.c
src/target/target.h

index ee302ee47c7a3673108ae2e9e9f4fe8d31318241..e04ecc470e937a485f49c0291817b6a9987eca98 100644 (file)
@@ -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,
index 76630b973fe0d20de05cb352f792851121e09807..53f9e26147c0494e43b2937230dd087fbd996d6c 100644 (file)
@@ -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? */

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)