swaybar: fix memory leaks

This commit is contained in:
progandy 2015-12-22 15:35:37 +01:00
parent d060d0a39f
commit 8fefdb3c64
3 changed files with 48 additions and 2 deletions

View file

@ -26,6 +26,15 @@ void list_free(list_t *list) {
free(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) { void list_add(list_t *list, void *item) {
list_resize(list); list_resize(list);
list->items[list->length++] = item; list->items[list->length++] = item;

View file

@ -9,6 +9,7 @@ typedef struct {
list_t *create_list(void); list_t *create_list(void);
void list_free(list_t *list); 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_add(list_t *list, void *item);
void list_insert(list_t *list, int index, void *item); void list_insert(list_t *list, int index, void *item);
void list_del(list_t *list, int index); void list_del(list_t *list, int index);

View file

@ -160,9 +160,21 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
(color & 0xFF) / 256.0); (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() { void ipc_update_workspaces() {
if (workspaces) { if (workspaces) {
free_flat_list(workspaces); list_foreach(workspaces, free_workspace);
list_free(workspaces);
} }
workspaces = create_list(); 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) { void parse_json(const char *text) {
/* the array of objects looks like this: /* the array of objects looks like this:
* [ { * [ {
@ -484,7 +519,8 @@ void parse_json(const char *text) {
} }
if (status_line) { if (status_line) {
free_flat_list(status_line); list_foreach(status_line, free_status_block);
list_free(status_line);
} }
status_line = create_list(); status_line = create_list();