diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index d22b2defa48a..67c492891562 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -20,6 +20,7 @@ extern Monitor *cur_mon; bool monitor_cur_is_qmp(void); void monitor_init(CharDriverState *chr, int flags); +void monitor_cleanup(void); int monitor_suspend(Monitor *mon); void monitor_resume(Monitor *mon); diff --git a/monitor.c b/monitor.c index b469225279e6..e214a58f6b98 100644 --- a/monitor.c +++ b/monitor.c @@ -635,6 +635,13 @@ static void monitor_data_init(Monitor *mon) static void monitor_data_destroy(Monitor *mon) { + if (mon->chr) { + qemu_chr_add_handlers(mon->chr, NULL, NULL, NULL, NULL); + } + if (monitor_is_qmp(mon)) { + json_message_parser_destroy(&mon->qmp.parser); + } + g_free(mon->rs); QDECREF(mon->outbuf); qemu_mutex_destroy(&mon->out_lock); } @@ -4156,6 +4163,19 @@ void monitor_init(CharDriverState *chr, int flags) qemu_mutex_unlock(&monitor_lock); } +void monitor_cleanup(void) +{ + Monitor *mon, *next; + + qemu_mutex_lock(&monitor_lock); + QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) { + QLIST_REMOVE(mon, entry); + monitor_data_destroy(mon); + g_free(mon); + } + qemu_mutex_unlock(&monitor_lock); +} + static void bdrv_password_cb(void *opaque, const char *password, void *readline_opaque) { diff --git a/vl.c b/vl.c index 56b05c6256a3..e3429427d99b 100644 --- a/vl.c +++ b/vl.c @@ -4687,6 +4687,7 @@ int main(int argc, char **argv, char **envp) #endif net_cleanup(); + monitor_cleanup(); qemu_chr_cleanup(); return 0;