From 8fefdb3c64d53a16bd400f97252c6887f5940af0 Mon Sep 17 00:00:00 2001 From: progandy Date: Tue, 22 Dec 2015 15:35:37 +0100 Subject: [PATCH] swaybar: fix memory leaks --- common/list.c | 9 +++++++++ include/list.h | 1 + swaybar/main.c | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common/list.c b/common/list.c index 850c8569..a7585a31 100644 --- a/common/list.c +++ b/common/list.c @@ -26,6 +26,15 @@ void list_free(list_t *list) { free(list); } +void list_foreach(list_t *list, void (*callback)(void *item)) { + if (list == NULL || callback == NULL) { + return; + } + for (int i = 0; i < list->length; i++) { + callback(list->items[i]); + } +} + void list_add(list_t *list, void *item) { list_resize(list); list->items[list->length++] = item; diff --git a/include/list.h b/include/list.h index d18d3f54..b2e26f95 100644 --- a/include/list.h +++ b/include/list.h @@ -9,6 +9,7 @@ typedef struct { list_t *create_list(void); void list_free(list_t *list); +void list_foreach(list_t *list, void (*callback)(void* item)); void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); diff --git a/swaybar/main.c b/swaybar/main.c index a4b757a5..75d043b0 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -160,9 +160,21 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { (color & 0xFF) / 256.0); } +void free_workspace(void *item) { + if (!item) { + return; + } + struct workspace *ws = (struct workspace *)item; + if (ws->name) { + free(ws->name); + } + free(ws); +} + void ipc_update_workspaces() { if (workspaces) { - free_flat_list(workspaces); + list_foreach(workspaces, free_workspace); + list_free(workspaces); } workspaces = create_list(); @@ -456,6 +468,29 @@ void render() { } } +void free_status_block(void *item) { + if (!item) { + return; + } + struct status_block *sb = (struct status_block*)item; + if (sb->full_text) { + free(sb->full_text); + } + if (sb->short_text) { + free(sb->short_text); + } + if (sb->align) { + free(sb->align); + } + if (sb->name) { + free(sb->name); + } + if (sb->instance) { + free(sb->instance); + } + free(sb); +} + void parse_json(const char *text) { /* the array of objects looks like this: * [ { @@ -484,7 +519,8 @@ void parse_json(const char *text) { } if (status_line) { - free_flat_list(status_line); + list_foreach(status_line, free_status_block); + list_free(status_line); } status_line = create_list();