image: Fix a memleak

Before this patch, this code allocated a buffer and then created an imlib image
from this via imlib_create_using_copied_data(), but this function does not take
ownership of the buffer you pass in and thus we had a memory leak.

This is fixed by using a separate function for creating "empty" images which
doesn't have this memory leak problem.

This patch was verified using a quick valgrind run and seemed to be fine.

Signed-off-by: Uli Schlachter <psychon@znc.in>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Uli Schlachter 2009-09-26 11:01:51 +02:00 committed by Julien Danjou
parent 539af16a65
commit 8165a3b483
1 changed files with 26 additions and 2 deletions

28
image.c
View File

@ -244,6 +244,31 @@ image_new_from_argb32(int width, int height, uint32_t *data)
return 0; return 0;
} }
/** Create a new, completely black image.
* \param width The image width.
* \param height The image height.
* \return 1 if an image has been pushed on stack, 0 otherwise.
*/
static int
image_new_blank(int width, int height)
{
Imlib_Image imimage;
if((imimage = imlib_create_image(width, height)))
{
imlib_context_set_image(imimage);
imlib_image_set_has_alpha(true);
/* After creation, an image has undefined content. Fix that up. */
imlib_context_set_color(0, 0, 0, 0xff);
imlib_image_fill_rectangle(0, 0, width, height);
image_t *image = image_new(globalconf.L);
image->image = imimage;
return 1;
}
return 0;
}
/** Load an image from filename. /** Load an image from filename.
* \param filename The image file to load. * \param filename The image file to load.
* \return 1 if image is loaded and on stack, 0 otherwise. * \return 1 if image is loaded and on stack, 0 otherwise.
@ -311,8 +336,7 @@ luaA_image_argb32_new(lua_State *L)
if(lua_isnil(L, 3)) if(lua_isnil(L, 3))
{ {
uint32_t *data = p_new(uint32_t, width * height); return image_new_blank(width, height);
return image_new_from_argb32(width, height, data);
} }
const char *data = luaL_checklstring(L, 3, &len); const char *data = luaL_checklstring(L, 3, &len);