Browse Source

No more crashes when there is no memory for newImageData (issue #209)

Bart van Strien 14 years ago
parent
commit
d55941f7fa
3 changed files with 29 additions and 2 deletions
  1. 1 0
      changes.txt
  2. 22 1
      src/modules/image/devil/ImageData.cpp
  3. 6 1
      src/modules/image/wrap_Image.cpp

+ 1 - 0
changes.txt

@@ -11,6 +11,7 @@ LOVE 0.7.2 [Game Slave]
   * Fixed files loaded by libmodplug being too loud.
   * Fixed paths with periods in them not working.
   * Fixed love.graphics.getBlendMode not detecting subtractive and multiplicative blend modes.
+  * Fixed crash when there was no memory available for newImageData(w, h).
 
   * Updated PhysicsFS version to 2.0.2 on Windows
   * Updated OpenAL Soft version to 1.13 on Windows

+ 22 - 1
src/modules/image/devil/ImageData.cpp

@@ -88,7 +88,28 @@ namespace devil
 		// Bind the image.
 		ilBindImage(image);
 
-		ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0);
+		bool success = ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0);
+		int err = ilGetError();
+		if (err != IL_NO_ERROR){
+			switch (err) {
+				case IL_ILLEGAL_OPERATION:
+					throw love::Exception("Error: Illegal operation");
+					break;
+				case IL_INVALID_PARAM:
+					throw love::Exception("Error: invalid parameters");
+					break;
+				case IL_OUT_OF_MEMORY:
+					throw love::Exception("Error: out of memory");
+					break;
+				default:
+					throw love::Exception("Error: unknown error");
+					break;
+			}
+		}
+
+		if(!success) {
+			throw love::Exception("Could not decode image data.");
+		}
 
 		// Set to black.
 		memset((void*)ilGetData(), 0, width*height*4);

+ 6 - 1
src/modules/image/wrap_Image.cpp

@@ -43,7 +43,12 @@ namespace image
 		{
 			int w = luaL_checkint(L, 1);
 			int h = luaL_checkint(L, 2);
-			ImageData * t = instance->newImageData(w, h);
+			ImageData * t = 0;
+			try {
+				t = instance->newImageData(w, h);
+			} catch (love::Exception & e) {
+				return luaL_error(L, e.what());
+			}
 			luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
 			return 1;
 		}