Image createCopy() leaking memory

Using the createCopy() command it is leaking memory. If you check “getReferenceCount()” on the copy it is 2. Here is a code snippet":

Image getSmallerImage(File file){
Image img = ImageFileFormat::loadFrom(file);
Rectangle r (…);
Image retImage = img.getClippedImage®.createCopy();//Needed to get rid of reference to larger image
//Here a retImage.getReferenceCount() = 2;
return retImage;

Without the createCopy, all works great except that I am using way too much memory as I only need a small part of each large image. The leak detector also reports the leaks in CoreGraphicsImage.

If I add a:
after the createCopy(), it all works fine.

Thanks for the heads-up - try again now on the develop branch…

Image getSmallerImage(File file){ // hit cache Image img = ImageCache::getFromFile(file); juce::Rectangle<int> r (0,0, 16, 16); // create new image surface, clipped to smaller size Image newImage (img.getFormat(), r.getWidth(), r.getHeight(), true); { Graphics g (newImage); g.drawImageAt (img, -r.getX(), -r.getY()); // copy sub section } return newImage; }

Thanks, your update works perfectly.

Sure, this would work too, but the OP was reporting a genuine bug. And now that I’ve fixed the bug, his original clone() version is the best way to do this task.

Yepp, I had also posted a workaround for the bug, but since you’ve fixed it, only this section of my reply was of any use.

You have a typo which broke the windows build:

ImagePixelData:Ptr clone() override

should be:

ImagePixelData::Ptr clone() override


doh! Thanks, fixed now!