Browse Source

Fixed memory leak in EncodedImageData, thanks Michael R

Bart van Strien 15 years ago
parent
commit
2107f6810b

+ 21 - 10
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<EncodedImageData::Format, EncodedImageData::FORMAT_MAX_ENUM>::Entry EncodedImageData::formatEntries[] = 
+	StringMap<EncodedImageData::Format, EncodedImageData::FORMAT_MAX_ENUM>::Entry EncodedImageData::formatEntries[] =
 	{
 		{"tga", EncodedImageData::FORMAT_TGA},
 		{"bmp", EncodedImageData::FORMAT_BMP},

+ 20 - 17
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 <common/StringMap.h>
 
 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<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
 		static StringMap<Format, FORMAT_MAX_ENUM> formats;
-		
+
+		void (*freeData)(void *data);
+
 	}; // EncodedImageData
-	
+
 } // image
 } // love
 

+ 16 - 11
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

+ 7 - 6
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 <string.h>
 
 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