Image createCopy() leaking memory


#1

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.


#2

If I add a:
retImg.getPixelData()->decReferenceCount();
after the createCopy(), it all works fine.


#3

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


#4

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; }


#5

Jules,
Thanks, your update works perfectly.


#6

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.


#7

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.


#8

Jules,
You have a typo which broke the windows build:

ImagePixelData:Ptr clone() override

should be:

ImagePixelData::Ptr clone() override

Thanks,
Darren


#9

doh! Thanks, fixed now!