From 7741adc7b4d2faaf021c30256992d5c50ade5e21 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 19 Mar 2015 17:08:44 -0300 Subject: [PATCH] Switched back to zlib decompression for PNG images and fixed a memory leak issue when decompressing. --- src/modules/image/magpie/PNGHandler.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/modules/image/magpie/PNGHandler.cpp b/src/modules/image/magpie/PNGHandler.cpp index f809e15aa..314ff847a 100644 --- a/src/modules/image/magpie/PNGHandler.cpp +++ b/src/modules/image/magpie/PNGHandler.cpp @@ -51,7 +51,7 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig uLongf outdatasize = insize; size_t sizemultiplier = 0; - unsigned char *outdata = nullptr; + unsigned char *outdata = out != nullptr ? *out : nullptr; while (true) { @@ -59,7 +59,13 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig outdatasize = insize << (++sizemultiplier); // LodePNG uses malloc, realloc, and free. - outdata = (unsigned char *) malloc(outdatasize); + // Since version 2014-08-23, LodePNG passes in an existing pointer in + // the 'out' argument that it expects to be realloc'd. Not doing so can + // result in a memory leak. + if (outdata != nullptr) + outdata = (unsigned char *) realloc(outdata, outdatasize); + else + outdata = (unsigned char *) malloc(outdatasize); if (!outdata) return 83; // "Memory allocation failed" error code for LodePNG. @@ -152,11 +158,7 @@ PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata) state.info_raw.colortype = LCT_RGBA; state.info_raw.bitdepth = 8; -#if 0 - // FIXME: temporarily disabled: using this makes decoded images use more - // memory than they should and seems to potentially cause memory leaks. state.decoder.zlibsettings.custom_zlib = zlibDecompress; -#endif unsigned status = lodepng_decode(&img.data, &width, &height, &state, indata, insize);