diff --git a/src/modules/image/CompressedData.cpp b/src/modules/image/CompressedData.cpp index 610cd28ed..c54cdfe72 100644 --- a/src/modules/image/CompressedData.cpp +++ b/src/modules/image/CompressedData.cpp @@ -50,7 +50,7 @@ void *CompressedData::getData() const // Data for different mipmap levels is not stored contiguously in memory, so // getData() won't work properly for CompressedData. if (dataImages.size() > 0 && dataImages[0].size > 0) - return (void *) &(dataImages[0].data[0]); + return (void *) dataImages[0].data; else return 0; } diff --git a/src/modules/image/CompressedData.h b/src/modules/image/CompressedData.h index fb5b68b69..f1a8ca85c 100644 --- a/src/modules/image/CompressedData.h +++ b/src/modules/image/CompressedData.h @@ -63,7 +63,7 @@ public: { int width, height; size_t size; - std::vector data; + char *data; }; CompressedData(); diff --git a/src/modules/image/magpie/CompressedData.cpp b/src/modules/image/magpie/CompressedData.cpp index 5334112eb..fa87e2025 100644 --- a/src/modules/image/magpie/CompressedData.cpp +++ b/src/modules/image/magpie/CompressedData.cpp @@ -36,6 +36,9 @@ CompressedData::CompressedData(love::filesystem::FileData *data) CompressedData::~CompressedData() { + // We have ownership of the heap memory in dataImages, so we have to free it. + for (size_t i = 0; i < dataImages.size(); i++) + delete[] dataImages[i].data; } void CompressedData::load(love::filesystem::FileData *data) @@ -53,6 +56,13 @@ void CompressedData::load(love::filesystem::FileData *data) if (parsedimages.size() == 0) throw love::Exception("Could not parse compressed data: No valid data?"); + // Make sure to clean up any previously loaded data. + for (size_t i = 0; i < dataImages.size(); i++) + { + delete[] dataImages[i].data; + dataImages[i].data = 0; + } + dataImages = parsedimages; type = textype; } diff --git a/src/modules/image/magpie/DevilHandler.cpp b/src/modules/image/magpie/DevilHandler.cpp index 739fce4c1..a2e148ab3 100644 --- a/src/modules/image/magpie/DevilHandler.cpp +++ b/src/modules/image/magpie/DevilHandler.cpp @@ -54,6 +54,11 @@ void DevilHandler::init() void DevilHandler::quit() { ilShutDown(); + if (devilMutex) + { + delete devilMutex; + devilMutex = 0; + } } bool DevilHandler::canDecode(love::filesystem::FileData * /*data*/) @@ -210,9 +215,10 @@ DevilHandler::EncodedImage DevilHandler::encode(const DecodedImage &img, ImageDa } catch (std::exception &e) { - // catches love and std exceptions + // Catches love and std exceptions. ilDeleteImage(tempimage); delete[] encodedimage.data; + encodedimage.data = 0; throw love::Exception("%s", e.what()); } diff --git a/src/modules/image/magpie/ddsHandler.cpp b/src/modules/image/magpie/ddsHandler.cpp index 490caf865..134fcf72a 100644 --- a/src/modules/image/magpie/ddsHandler.cpp +++ b/src/modules/image/magpie/ddsHandler.cpp @@ -44,6 +44,7 @@ CompressedData::TextureType ddsHandler::parse(filesystem::FileData *data, std::v try { + // Attempt to parse the dds file. dds::Parser parser(data->getData(), data->getSize()); textype = convertFormat(parser.getFormat()); @@ -54,8 +55,10 @@ CompressedData::TextureType ddsHandler::parse(filesystem::FileData *data, std::v if (parser.getMipmapCount() == 0) throw love::Exception("Could not parse compressed data: No readable texture data."); + // Copy the parsed mipmap levels from the FileData to our CompressedData. for (size_t i = 0; i < parser.getMipmapCount(); i++) { + // Fetch the data for this mipmap level. const dds::Image *img = parser.getImageData(i); CompressedData::SubImage mip; @@ -63,13 +66,20 @@ CompressedData::TextureType ddsHandler::parse(filesystem::FileData *data, std::v mip.width = img->width; mip.height = img->height; mip.size = img->dataSize; - mip.data.insert(mip.data.begin(), &img->data[0], &img->data[mip.size]); + + // Copy the mipmap image from the FileData. + mip.data = new char[mip.size]; + memcpy(mip.data, img->data, mip.size); images.push_back(mip); } } catch (std::exception &e) { + // Clean up any newly allocated heap memory before throwing. + for (size_t i = 0; i < images.size(); i++) + delete[] images[i].data; + throw love::Exception(e.what()); }