* if we malloc we should free!

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1501 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-04-18 20:06:35 +00:00
parent 108e9ec935
commit 68eaeab5f3
1 changed files with 9 additions and 6 deletions

View File

@ -375,23 +375,26 @@ static void wzpng_flush_data(png_structp png_ptr)
// End of PNG callbacks
static inline void PNGCleanup(png_infop *info_ptr, png_structp *png_ptr)
static inline void PNGCleanup(png_infop *info_ptr, png_structp *png_ptr, const unsigned char** scanlines)
{
if (*info_ptr != NULL)
png_destroy_info_struct(*png_ptr, info_ptr);
if (*png_ptr != NULL)
png_destroy_read_struct(png_ptr, NULL, NULL);
if (scanlines != NULL)
free(scanlines);
}
static inline void screen_DumpPNG(PHYSFS_file* fileHandle, const unsigned char* inputBuffer, unsigned int width, unsigned int height, unsigned int channels)
{
const unsigned char** scanlines = NULL;
png_infop info_ptr = NULL;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
debug(LOG_ERROR, "screen_DumpPNG: Unable to create png struct\n");
return PNGCleanup(&info_ptr, &png_ptr);
return PNGCleanup(&info_ptr, &png_ptr, scanlines);
}
info_ptr = png_create_info_struct(png_ptr);
@ -399,7 +402,7 @@ static inline void screen_DumpPNG(PHYSFS_file* fileHandle, const unsigned char*
if (info_ptr == NULL)
{
debug(LOG_ERROR, "screen_DumpPNG: Unable to create png info struct\n");
return PNGCleanup(&info_ptr, &png_ptr);
return PNGCleanup(&info_ptr, &png_ptr, scanlines);
}
// If libpng encounters an error, it will jump into this if-branch
@ -411,12 +414,12 @@ static inline void screen_DumpPNG(PHYSFS_file* fileHandle, const unsigned char*
{
unsigned int currentRow;
unsigned int row_stride = width * channels;
const unsigned char** scanlines = malloc(sizeof(const unsigned char*) * height);
scanlines = malloc(sizeof(const unsigned char*) * height);
if (scanlines == NULL)
{
debug(LOG_ERROR, "screen_DumpPNG: Couldn't allocate memory\n");
return PNGCleanup(&info_ptr, &png_ptr);
return PNGCleanup(&info_ptr, &png_ptr, scanlines);
}
png_set_write_fn(png_ptr, fileHandle, wzpng_write_data, wzpng_flush_data);
@ -463,7 +466,7 @@ static inline void screen_DumpPNG(PHYSFS_file* fileHandle, const unsigned char*
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
}
return PNGCleanup(&info_ptr, &png_ptr);
return PNGCleanup(&info_ptr, &png_ptr, scanlines);
}
/** Retrieves the currently displayed screen and throws it in a buffer