diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index 45b7394c4..d21753c7b 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -652,10 +652,10 @@ love::image::ImageData *Canvas::getImageData(love::image::Image *image) for (int i = 0; i < height; ++i, dst -= row, src += row) memcpy(dst, src, row); - love::image::ImageData *img = image->newImageData(width, height, (void *)flipped); + love::image::ImageData *img = image->newImageData(width, height, (void *)flipped, true); + // The new ImageData now owns the flipped data, so we don't delete it here. delete[] pixels; - delete[] flipped; return img; } diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 11e588c59..f2ebd221b 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -1201,7 +1201,9 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool love::image::ImageData *img = 0; try { - img = image->newImageData(w, h, (void *) screenshot); + // Tell the new ImageData that it owns the screenshot data, so we don't + // need to delete it here. + img = image->newImageData(w, h, (void *) screenshot, true); } catch (love::Exception &) { @@ -1211,8 +1213,6 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool throw; } - delete[] screenshot; - // Re-bind the active canvas, if necessary. if (curcanvas) curcanvas->startGrab(curcanvas->getAttachedCanvases()); diff --git a/src/modules/image/Image.h b/src/modules/image/Image.h index 5d866feb0..a319affb1 100644 --- a/src/modules/image/Image.h +++ b/src/modules/image/Image.h @@ -69,9 +69,11 @@ public: * @param The width of the ImageData. * @param The height of the ImageData. * @param The data to load into the ImageData. + * @param Whether the new ImageData should take ownership of the data or + * copy it. * @return The new ImageData. **/ - virtual ImageData *newImageData(int width, int height, void *data) = 0; + virtual ImageData *newImageData(int width, int height, void *data, bool own = false) = 0; /** * Creates new CompressedData from FileData. diff --git a/src/modules/image/magpie/Image.cpp b/src/modules/image/magpie/Image.cpp index 38d79fe65..acf2a07ca 100644 --- a/src/modules/image/magpie/Image.cpp +++ b/src/modules/image/magpie/Image.cpp @@ -57,9 +57,9 @@ love::image::ImageData *Image::newImageData(int width, int height) return new ImageData(width, height); } -love::image::ImageData *Image::newImageData(int width, int height, void *data) +love::image::ImageData *Image::newImageData(int width, int height, void *data, bool own) { - return new ImageData(width, height, data); + return new ImageData(width, height, data, own); } love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data) diff --git a/src/modules/image/magpie/Image.h b/src/modules/image/magpie/Image.h index d0ea76713..77e04ccc9 100644 --- a/src/modules/image/magpie/Image.h +++ b/src/modules/image/magpie/Image.h @@ -48,7 +48,7 @@ public: love::image::ImageData *newImageData(love::filesystem::FileData *data); love::image::ImageData *newImageData(int width, int height); - love::image::ImageData *newImageData(int width, int height, void *data); + love::image::ImageData *newImageData(int width, int height, void *data, bool own = false); love::image::CompressedData *newCompressedData(love::filesystem::FileData *data); diff --git a/src/modules/image/magpie/ImageData.cpp b/src/modules/image/magpie/ImageData.cpp index b2e878321..8cc75751f 100644 --- a/src/modules/image/magpie/ImageData.cpp +++ b/src/modules/image/magpie/ImageData.cpp @@ -45,11 +45,15 @@ ImageData::ImageData(int width, int height) memset(data, 0, width*height*sizeof(pixel)); } -ImageData::ImageData(int width, int height, void *data) +ImageData::ImageData(int width, int height, void *data, bool own) { this->width = width; this->height = height; - create(width, height, data); + + if (own) + this->data = (unsigned char *) data; + else + create(width, height, data); } ImageData::~ImageData() diff --git a/src/modules/image/magpie/ImageData.h b/src/modules/image/magpie/ImageData.h index 64eafa86a..f2e1413a8 100644 --- a/src/modules/image/magpie/ImageData.h +++ b/src/modules/image/magpie/ImageData.h @@ -38,7 +38,7 @@ public: ImageData(love::filesystem::FileData *data); ImageData(int width, int height); - ImageData(int width, int height, void *data); + ImageData(int width, int height, void *data, bool own); virtual ~ImageData(); // Implements image::ImageData.