From 2107f6810bff64cc055ce8403aadbf48a13191fd Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Tue, 7 Sep 2010 17:16:58 +0200 Subject: [PATCH] Fixed memory leak in EncodedImageData, thanks Michael R --- src/modules/image/EncodedImageData.cpp | 31 ++++++++++++++------- src/modules/image/EncodedImageData.h | 37 ++++++++++++++------------ src/modules/image/devil/ImageData.cpp | 27 +++++++++++-------- src/modules/image/devil/ImageData.h | 13 ++++----- 4 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/modules/image/EncodedImageData.cpp b/src/modules/image/EncodedImageData.cpp index 386caf9cb..aed41a692 100644 --- a/src/modules/image/EncodedImageData.cpp +++ b/src/modules/image/EncodedImageData.cpp @@ -1,14 +1,14 @@ /** * Copyright (c) 2006-2010 LOVE Development Team - * + * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. - * + * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: - * + * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be @@ -25,21 +25,32 @@ namespace love namespace image { EncodedImageData::EncodedImageData(void * d, Format f, int s) - : data(d), size(s), format(f) + : data(d), size(s), format(f), freeData(0) { } - - void * EncodedImageData::getData() const + + EncodedImageData::EncodedImageData(void * d, Format f, int s, void (*freeData)(void*)) + : data(d), size(s), format(f), freeData(freeData) + { + } + + EncodedImageData::~EncodedImageData() + { + if (freeData != 0) + freeData(data); + } + + void * EncodedImageData::getData() const { return data; } - + EncodedImageData::Format EncodedImageData::getFormat() const { return format; } - - int EncodedImageData::getSize() const + + int EncodedImageData::getSize() const { return size; } @@ -54,7 +65,7 @@ namespace image return formats.find(in, out); } - StringMap::Entry EncodedImageData::formatEntries[] = + StringMap::Entry EncodedImageData::formatEntries[] = { {"tga", EncodedImageData::FORMAT_TGA}, {"bmp", EncodedImageData::FORMAT_BMP}, diff --git a/src/modules/image/EncodedImageData.h b/src/modules/image/EncodedImageData.h index 7a1d60edc..cfb5a6d47 100644 --- a/src/modules/image/EncodedImageData.h +++ b/src/modules/image/EncodedImageData.h @@ -1,14 +1,14 @@ /** * Copyright (c) 2006-2010 LOVE Development Team - * + * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. - * + * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: - * + * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be @@ -26,17 +26,17 @@ #include namespace love -{ +{ namespace image { /** - * Represents encoded pixel data. + * Represents encoded pixel data. **/ class EncodedImageData : public Data { public: - - enum Format + + enum Format { FORMAT_TGA = 1, FORMAT_BMP, @@ -47,16 +47,17 @@ namespace image * Constructor. **/ EncodedImageData(void * data, Format format, int size); - + EncodedImageData(void * data, Format format, int size, void (*freeData)(void*)); + /** * Destructor. **/ - virtual ~EncodedImageData(){}; - + virtual ~EncodedImageData(); + // Implements Data. void * getData() const; int getSize() const; - + /** * Get the format the data is encoded in. **/ @@ -64,19 +65,19 @@ namespace image static bool getConstant(const char * in, Format & out); static bool getConstant(Format in, const char *& out); - + private: - + /** * Actual data. **/ void * data; - + /** * Size of the data. **/ int size; - + /** * Image format. **/ @@ -84,9 +85,11 @@ namespace image static StringMap::Entry formatEntries[]; static StringMap formats; - + + void (*freeData)(void *data); + }; // EncodedImageData - + } // image } // love diff --git a/src/modules/image/devil/ImageData.cpp b/src/modules/image/devil/ImageData.cpp index eedc97954..d8ec2a3e4 100644 --- a/src/modules/image/devil/ImageData.cpp +++ b/src/modules/image/devil/ImageData.cpp @@ -1,14 +1,14 @@ /** * Copyright (c) 2006-2010 LOVE Development Team -* +* * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. -* +* * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: -* +* * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be @@ -86,14 +86,14 @@ namespace devil ilGenImages(1, &image); // Bind the image. - ilBindImage(image); + ilBindImage(image); ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0); // Set to black. memset((void*)ilGetData(), 0, width*height*4); } - + ImageData::ImageData(int width, int height, void *data) : width(width), height(height), origin(IL_ORIGIN_UPPER_LEFT), bpp(4) { @@ -120,7 +120,7 @@ namespace devil break; } } - + if(!success) { throw love::Exception("Could not decode image data."); } @@ -131,12 +131,12 @@ namespace devil ilDeleteImages(1, &image); } - int ImageData::getWidth() const + int ImageData::getWidth() const { return width; } - int ImageData::getHeight() const + int ImageData::getHeight() const { return height; } @@ -167,7 +167,7 @@ namespace devil pixel * pixels = (pixel *)getData(); return pixels[y*width+x]; } - + EncodedImageData * ImageData::encode(EncodedImageData::Format f) { ILubyte * data; ILuint w = getWidth(); @@ -258,7 +258,7 @@ namespace devil // header done. write the pixel data to TGA: data += headerLen; ilCopyPixels(0,0,0,w,h,1,IL_BGR,IL_UNSIGNED_BYTE,data); // convert the pixels to BGR (remember, little-endian) and copy them to data - + // It's Targa, so we have to flip the image. row = w * bpp; ILubyte * temp = new ILubyte[row]; @@ -272,7 +272,12 @@ namespace devil data -= headerLen; delete [] temp; } - return new EncodedImageData(data, f, size + headerLen); + return new EncodedImageData(data, f, size + headerLen, freeData); + } + + void ImageData::freeData(void *data) + { + delete[] (ILubyte*) data; } } // devil diff --git a/src/modules/image/devil/ImageData.h b/src/modules/image/devil/ImageData.h index e2c9ccc80..b7ad72cf7 100644 --- a/src/modules/image/devil/ImageData.h +++ b/src/modules/image/devil/ImageData.h @@ -1,14 +1,14 @@ /** * Copyright (c) 2006-2010 LOVE Development Team -* +* * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. -* +* * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: -* +* * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be @@ -34,7 +34,7 @@ #include namespace love -{ +{ namespace image { namespace devil @@ -77,8 +77,9 @@ namespace devil int getHeight() const ; void setPixel(int x, int y, pixel c); pixel getPixel(int x, int y) const; - EncodedImageData * encode(EncodedImageData::Format f); - + EncodedImageData * encode(EncodedImageData::Format f); + + static void freeData(void *data); }; // ImageData