From e91a9174e24d8fe0616115d777f8ee641e50a782 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 10 Feb 2015 15:11:40 -0500 Subject: [PATCH] libi3: free previous font on font load When loading a new font with `load_font`, free the previously loaded font with `free_font`. If no font is loaded, `free_font` will simply return (instead of crashing because of a double free). --- include/libi3.h | 6 ++++-- libi3/font.c | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/libi3.h b/include/libi3.h index 7c0ead32..7a2bdf58 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -290,7 +290,8 @@ uint32_t get_mod_mask_for(uint32_t keysym, /** * Loads a font for usage, also getting its height. If fallback is true, - * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. + * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. If any + * font was previously loaded, it will be freed. * */ i3Font load_font(const char *pattern, const bool fallback); @@ -302,7 +303,8 @@ i3Font load_font(const char *pattern, const bool fallback); void set_font(i3Font *font); /** - * Frees the resources taken by the current font. + * Frees the resources taken by the current font. If no font was previously + * loaded, it simply returns. * */ void free_font(void); diff --git a/libi3/font.c b/libi3/font.c index 7670335f..a338f975 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -160,10 +160,14 @@ static int predict_text_width_pango(const char *text, size_t text_len) { /* * Loads a font for usage, also getting its metrics. If fallback is true, - * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. + * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. If any + * font was previously loaded, it will be freed. * */ i3Font load_font(const char *pattern, const bool fallback) { + /* if any font was previously loaded, free it now */ + free_font(); + i3Font font; font.type = FONT_TYPE_NONE; @@ -257,10 +261,15 @@ void set_font(i3Font *font) { } /* - * Frees the resources taken by the current font. + * Frees the resources taken by the current font. If no font was previously + * loaded, it simply returns. * */ void free_font(void) { + /* if there is no saved font, simply return */ + if (savedFont == NULL) + return; + free(savedFont->pattern); switch (savedFont->type) { case FONT_TYPE_NONE: @@ -283,6 +292,8 @@ void free_font(void) { assert(false); break; } + + savedFont = NULL; } /* -- 2.39.5