From f511cc61c18a8c73a58e26dd8d5b89a323e2aac5 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 9 Jan 2016 16:47:48 +0100 Subject: [PATCH] fix memory leak when swallowing windows --- include/window.h | 6 ++++++ src/manage.c | 3 +++ src/tree.c | 7 ++----- src/window.c | 12 ++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/window.h b/include/window.h index 7a248277..d0b97f1d 100644 --- a/include/window.h +++ b/include/window.h @@ -9,6 +9,12 @@ */ #pragma once +/** + * Frees an i3Window and all its members. + * + */ +void window_free(i3Window *win); + /** * Updates the WM_CLASS (consisting of the class and instance) for the * given window. diff --git a/src/manage.c b/src/manage.c index 8e68c51e..2bcb47f3 100644 --- a/src/manage.c +++ b/src/manage.c @@ -310,6 +310,9 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki } } } + if (nc->window != cwindow && nc->window != NULL) { + window_free(nc->window); + } nc->window = cwindow; x_reinit(nc); diff --git a/src/tree.c b/src/tree.c index 5ff426ca..0c301209 100644 --- a/src/tree.c +++ b/src/tree.c @@ -266,11 +266,8 @@ bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_par add_ignore_event(cookie.sequence, 0); } ipc_send_window_event("close", con); - FREE(con->window->class_class); - FREE(con->window->class_instance); - i3string_free(con->window->name); - FREE(con->window->ran_assignments); - FREE(con->window); + window_free(con->window); + con->window = NULL; } Con *ws = con_get_workspace(con); diff --git a/src/window.c b/src/window.c index bd4e2719..d6136c78 100644 --- a/src/window.c +++ b/src/window.c @@ -11,6 +11,18 @@ */ #include "all.h" +/* + * Frees an i3Window and all its members. + * + */ +void window_free(i3Window *win) { + FREE(win->class_class); + FREE(win->class_instance); + i3string_free(win->name); + FREE(win->ran_assignments); + FREE(win); +} + /* * Updates the WM_CLASS (consisting of the class and instance) for the * given window. -- 2.39.2