From 13ed6301d86af984364fce86640a9d23f6f0fb66 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sun, 29 Oct 2017 13:40:43 +0100 Subject: [PATCH 1/3] Add separate function to free RawImage --- src/notification.c | 19 ++++++++++++++----- src/notification.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/notification.c b/src/notification.c index 145e0c4..45e151d 100644 --- a/src/notification.c +++ b/src/notification.c @@ -163,6 +163,19 @@ int notification_is_duplicate(const notification *a, const notification *b) && a->urgency == b->urgency; } +/* + * Free a #RawImage + * @i: (nullable): pointer to #RawImage + */ +void rawimage_free(RawImage *i) +{ + if (!i) + return; + + g_free(i->data); + g_free(i); +} + /* * Free the memory used by the given notification. */ @@ -184,11 +197,7 @@ void notification_free(notification *n) g_free(n->actions->dmenu_str); } - if (n->raw_icon) { - if (n->raw_icon->data) - g_free(n->raw_icon->data); - g_free(n->raw_icon); - } + rawimage_free(n->raw_icon); g_free(n); } diff --git a/src/notification.h b/src/notification.h index e5250c1..fa3effa 100644 --- a/src/notification.h +++ b/src/notification.h @@ -63,6 +63,7 @@ typedef struct _notification { notification *notification_create(void); void notification_init(notification *n); +void rawimage_free(RawImage *i); void notification_free(notification *n); int notification_cmp(const void *a, const void *b); int notification_cmp_data(const void *a, const void *b, void *data); From 23cae3110d932b652aa524d32ceee9b9b2d5eec1 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sun, 29 Oct 2017 13:42:25 +0100 Subject: [PATCH 2/3] Optimize out icon_overridden There is no need save if the icon field should have precedence, as setting raw_icon to NULL emphasizes the same. Also freeing raw_icon saves unneccessary memory. --- src/notification.h | 1 - src/rules.c | 3 ++- src/x11/x.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/notification.h b/src/notification.h index fa3effa..d02f929 100644 --- a/src/notification.h +++ b/src/notification.h @@ -33,7 +33,6 @@ typedef struct _notification { char *appname; char *summary; char *body; - bool icon_overridden; char *icon; RawImage *raw_icon; char *msg; /* formatted message */ diff --git a/src/rules.c b/src/rules.c index fa30c7c..a9455b2 100644 --- a/src/rules.c +++ b/src/rules.c @@ -25,7 +25,8 @@ void rule_apply(rule_t *r, notification *n) if (r->new_icon) { g_free(n->icon); n->icon = g_strdup(r->new_icon); - n->icon_overridden = true; + rawimage_free(n->raw_icon); + n->raw_icon = NULL; } if (r->fg) n->color_strings[ColFG] = r->fg; diff --git a/src/x11/x.c b/src/x11/x.c index 3647222..0710b67 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -425,7 +425,7 @@ static colored_layout *r_init_shared(cairo_t *c, notification *n) GdkPixbuf *pixbuf = NULL; - if (n->raw_icon && !n->icon_overridden && + if (n->raw_icon && settings.icon_position != icons_off) { pixbuf = get_pixbuf_from_raw_image(n->raw_icon); From b571698f522192a62eae7e64f1b9f54844e97e81 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sun, 29 Oct 2017 16:17:50 +0100 Subject: [PATCH 3/3] Free Actions in separate method Also free the memory of the actual Action. --- src/dbus.c | 11 +++++------ src/notification.c | 20 +++++++++++++++----- src/notification.h | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index b669b70..01ac877 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -267,13 +267,12 @@ static void on_notify(GDBusConnection *connection, n->category = category; n->dbus_client = g_strdup(sender); n->transient = transient; - if (actions->count > 0) { - n->actions = actions; - } else { - n->actions = NULL; - g_strfreev(actions->actions); - g_free(actions); + + if (actions->count < 1) { + actions_free(actions); + actions = NULL; } + n->actions = actions; for (int i = 0; i < ColLast; i++) { n->color_strings[i] = NULL; diff --git a/src/notification.c b/src/notification.c index 45e151d..5b71ebb 100644 --- a/src/notification.c +++ b/src/notification.c @@ -163,6 +163,20 @@ int notification_is_duplicate(const notification *a, const notification *b) && a->urgency == b->urgency; } +/* + * Free the actions element + * @a: (nullable): Pointer to #Actions + */ +void actions_free(Actions *a) +{ + if (!a) + return; + + g_strfreev(a->actions); + g_free(a->dmenu_str); + g_free(a); +} + /* * Free a #RawImage * @i: (nullable): pointer to #RawImage @@ -192,11 +206,7 @@ void notification_free(notification *n) g_free(n->text_to_render); g_free(n->urls); - if (n->actions) { - g_strfreev(n->actions->actions); - g_free(n->actions->dmenu_str); - } - + actions_free(n->actions); rawimage_free(n->raw_icon); g_free(n); diff --git a/src/notification.h b/src/notification.h index d02f929..676548d 100644 --- a/src/notification.h +++ b/src/notification.h @@ -62,6 +62,7 @@ typedef struct _notification { notification *notification_create(void); void notification_init(notification *n); +void actions_free(Actions *a); void rawimage_free(RawImage *i); void notification_free(notification *n); int notification_cmp(const void *a, const void *b);