From e4d5d4702d8859b69119f8dfb6e06827d9992da0 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 4 Oct 2006 01:18:06 +0000 Subject: image widget memory leaks fixed (thanks to Magne Torresen) git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@721 3ae390bd-cb1e-0410-b409-cd5a39f66f1f --- widget_image.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/widget_image.c b/widget_image.c index 17d73de..cba52d7 100644 --- a/widget_image.c +++ b/widget_image.c @@ -1,4 +1,4 @@ -/* $Id: widget_image.c,v 1.10 2006/09/07 09:06:25 reinelt Exp $ +/* $Id: widget_image.c,v 1.11 2006/09/28 04:08:33 reinelt Exp $ * * image widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget_image.c,v $ + * Revision 1.11 2006/09/28 04:08:33 reinelt + * image widget memory leaks fixed (thanks to Magne Tørresen) + * * Revision 1.10 2006/09/07 09:06:25 reinelt * lots of wrong printf formats corrected (thanks to Ernst Bachmann) * @@ -110,25 +113,25 @@ static void widget_image_render(const char *Name, WIDGET_IMAGE * Image) if (Image->bitmap) { int i; for (i = 0; i < Image->height * Image->width; i++) { - RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 }; + RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 }; Image->bitmap[i] = empty; } } - + if (Image->file == NULL || Image->file[0] == '\0') { error("Warning: Image %s has no file", Name); return; } - + fd = fopen(Image->file, "rb"); if (fd == NULL) { error("Warning: Image %s: fopen(%s) failed: %s", Name, Image->file, strerror(errno)); return; } - + gdImage = gdImageCreateFromPng(fd); fclose(fd); - + if (fd == NULL) { error("Warning: Image %s: CreateFromPng(%s) failed!", Name, Image->file); return; @@ -137,14 +140,12 @@ static void widget_image_render(const char *Name, WIDGET_IMAGE * Image) /* maybe resize bitmap */ if (gdImage->sx > Image->width) { Image->width = gdImage->sx; - if (Image->bitmap) - free(Image->bitmap); + free(Image->bitmap); Image->bitmap = NULL; } if (gdImage->sy > Image->height) { Image->height = gdImage->sy; - if (Image->bitmap) - free(Image->bitmap); + free(Image->bitmap); Image->bitmap = NULL; } if (Image->bitmap == NULL && Image->width > 0 && Image->height > 0) { @@ -155,11 +156,11 @@ static void widget_image_render(const char *Name, WIDGET_IMAGE * Image) return; } for (i = 0; i < Image->height * Image->width; i++) { - RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 }; + RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 }; Image->bitmap[i] = empty; } } - + /* finally really render it */ if (Image->visible) { for (x = 0; x < gdImage->sx; x++) { @@ -181,6 +182,10 @@ static void widget_image_render(const char *Name, WIDGET_IMAGE * Image) } } } + + /* free image */ + gdImageDestroy(gdImage); + } @@ -194,10 +199,9 @@ static void widget_image_update(void *Self) if (W->parent == NULL) { /* evaluate expressions */ - if (Image->file) { - free(Image->file); - Image->file = NULL; - } + free(Image->file); + Image->file = NULL; + if (Image->file_tree != NULL) { Eval(Image->file_tree, &result); Image->file = strdup(R2S(&result)); @@ -239,8 +243,9 @@ static void widget_image_update(void *Self) W->class->draw(W); /* add a new one-shot timer */ - timer_add(widget_image_update, Self, Image->update, 1); - + if (Image->update > 0) { + timer_add(widget_image_update, Self, Image->update, 1); + } } @@ -313,10 +318,8 @@ int widget_image_quit(WIDGET * Self) if (Self->parent == NULL) { if (Self->data) { WIDGET_IMAGE *Image = Self->data; - if (Image->bitmap) - free(Image->bitmap); - if (Image->file) - free(Image->file); + free(Image->bitmap); + free(Image->file); DelTree(Image->file_tree); DelTree(Image->update_tree); DelTree(Image->visible_tree); -- cgit v1.2.3