From ade67faf797d0e055e512c78a0da4eba7650416f Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Fri, 26 May 2017 14:28:12 +0200 Subject: [PATCH] fix memory leaks when using g_hashtable when generating a bitmap for a character whose codepoint was above 255, we used a cache_id of 0, malloc'd a CachedImage and inserted it into a g_hashtable, without freeing the one which was before inserted with cache_id 0 this is circumvented by only generating a CachedImage when having a cache_id != 0 the second leak was also with inserting into a hashtable, but there we give the hashtable the g_free method as a value_destroy_func Signed-off-by: Dominik Csapak --- input.c | 4 ++-- screen.c | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/input.c b/input.c index 7fb5238..5ee82de 100644 --- a/input.c +++ b/input.c @@ -837,8 +837,8 @@ spiceterm_create(uint32_t width, uint32_t height, SpiceTermOptions *opts) SpiceCoreInterface *core = basic_event_loop_init(); SpiceScreen *spice_screen = spice_screen_new(core, width, height, opts); - keymap = g_hash_table_new(g_int_hash, g_int_equal); - + keymap = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, g_free); + if (!parse_keymap(opts->keymap ? opts->keymap : "en-us")) { return NULL; } diff --git a/screen.c b/screen.c index 9072dce..1a87e53 100644 --- a/screen.c +++ b/screen.c @@ -294,10 +294,13 @@ spice_screen_draw_char_cmd(SpiceScreen *spice_screen, int x, int y, int c, dst += 4; } } - ce = g_new(CachedImage, 1); - ce->cache_id = cache_id; - ce->bitmap = bitmap; - g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce); + + if (cache_id != 0) { + ce = g_new(CachedImage, 1); + ce->cache_id = cache_id; + ce->bitmap = bitmap; + g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce); + } } bbox.left = left; bbox.top = top; -- 2.39.2