diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index cf318c974..ec686287d 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -280,6 +280,29 @@ static dm013_info_t *get_dm(struct target *target) return dm; } +static void riscv013_dm_free(struct target *target) +{ + RISCV013_INFO(info); + dm013_info_t *dm = info->dm; + if (!dm) + return; + + target_list_t *target_entry; + list_for_each_entry(target_entry, &dm->target_list, list) { + if (target_entry->target == target) { + list_del(&target_entry->list); + free(target_entry); + break; + } + } + + if (list_empty(&dm->target_list)) { + list_del(&dm->list); + free(dm); + } + info->dm = NULL; +} + static riscv_debug_reg_ctx_t get_riscv_debug_reg_ctx(const struct target *target) { if (!target_was_examined(target)) { @@ -1864,6 +1887,8 @@ static void deinit_target(struct target *target) if (!info) return; + riscv013_dm_free(target); + free(info->version_specific); /* TODO: free register arch_info */ info->version_specific = NULL;