# Leaking Images

**URL:** <https://forum.juce.com/t/leaking-images/44263>\
**Category:** General JUCE discussion\
**Created:** [February 4, 2021, 8:05pm UTC](https://forum.juce.com/t/leaking-images/44263 "2021-02-04T20:05:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 4, 2021, 8:05pm UTC](https://forum.juce.com/t/leaking-images/44263/1 "2021-02-04T20:05:18Z")

</div>

Hi, I’m leaking a load of Image classes loaded with:

juce::ImageCache::getFromFile( file );

These are the only things leaked and I can’t figure out if I need to do anything to clean them up?

Thx

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [February 4, 2021, 9:17pm UTC](https://forum.juce.com/t/leaking-images/44263/2 "2021-02-04T21:17:44Z")

</div>

Hard to imagine. How do you observe the leaks?

The Image is internally reference counted.

Do you allocate the Images on the heap by any chance?  
via `new Image` or in a `std::unique_ptr`?

They are copyable at zero cost, so you should simply copy-assign them.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 4, 2021, 9:37pm UTC](https://forum.juce.com/t/leaking-images/44263/3 "2021-02-04T21:37:23Z")

</div>

hi daniel.

JUCE leak detector is showing them when i close down the app.

Don’t do anything other than call the above function.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [February 4, 2021, 9:43pm UTC](https://forum.juce.com/t/leaking-images/44263/4 "2021-02-04T21:43:45Z")

</div>

Did you click continue? The leaks are usually found cascading.

And do you add a `JUCE_LEAK_DETECTOR` or `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR` in all your classes?

I guess the leaking images are in fact inside an other leaked object.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 4, 2021, 9:57pm UTC](https://forum.juce.com/t/leaking-images/44263/5 "2021-02-04T21:57:34Z")

</div>

that’s what’s so strange - there’s nothing above them - just the leaking images.

gonna try it on windows and see what i get

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 4, 2021, 10:07pm UTC](https://forum.juce.com/t/leaking-images/44263/6 "2021-02-04T22:07:48Z")

</div>

hmmm… alot more stuff showing on Windows for some reason. will get that cleaned up tomorrow.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 8, 2021, 7:56am UTC](https://forum.juce.com/t/leaking-images/44263/7 "2021-02-08T07:56:26Z")

</div>

cleaned the other stuff up. still no idea on the images:

**8:55:08.935 \*\*\* Leaked objects detected: 17 instance(s) of class CoreGraphicsPixelData**

**8:55:08.935 JUCE Assertion failure in juce\_LeakedObjectDetector.h:92**

**8:55:11.161 \*\*\* Leaked objects detected: 17 instance(s) of class ImagePixelData**

**8:55:11.162 JUCE Assertion failure in juce\_LeakedObjectDetector.h:92**

**8:55:11.985 \*\*\* Leaked objects detected: 19 instance(s) of class Image**

**8:55:11.985 JUCE Assertion failure in juce\_LeakedObjectDetector.h:92**

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 9, 2021, 11:36am UTC](https://forum.juce.com/t/leaking-images/44263/8 "2021-02-09T11:36:17Z")

</div>

anyone any ideas on this one? thx

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 15, 2021, 10:09am UTC](https://forum.juce.com/t/leaking-images/44263/9 "2021-02-15T10:09:24Z")

</div>

@ed95 @t0m  
Hi - can someone from JUCE check there isn’t an issue here - I can’t see how to stop these leaks.

thx

---

<div class="post-metadata">

**Author:** ![mxs](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mxs](https://forum.juce.com/u/mxs)\
**Post date:** [February 15, 2021, 10:56am UTC](https://forum.juce.com/t/leaking-images/44263/10 "2021-02-15T10:56:55Z")

</div>

Hello,

Just a random guess, in the API there is an explicit function to clear the cache: [releaseUnusedImages](https://docs.juce.com/master/classImageCache.html#a59842f582bd240a820fb5e44b956b121)

Maybe you need to call it in the destructor of your PluginEditor?

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 15, 2021, 11:12am UTC](https://forum.juce.com/t/leaking-images/44263/11 "2021-02-15T11:12:21Z")

</div>

hi, I’m calling that in the class that does the loading of the images - maybe you’re right, that’s not good enough - I’ll try calling from the editor as you suggest.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 15, 2021, 11:19am UTC](https://forum.juce.com/t/leaking-images/44263/12 "2021-02-15T11:19:50Z")

</div>

no difference. also tried calling that from shutdown() of the Juce::Application and still leaking.  
looks like they’re still regarded as being in use for some reason…

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [February 15, 2021, 11:43am UTC](https://forum.juce.com/t/leaking-images/44263/13 "2021-02-15T11:43:06Z")

</div>

Just to rule out another obvious:  
You are not using any statics by any chance?  
Or Singletons?  
When using Singletons remember to inherit from [juce::DeletedAtShutdown](https://docs.juce.com/develop/classDeletedAtShutdown.html)

juce::SharedResourcePointer should handle the destruction by itself, but maybe have a look there too

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 15, 2021, 11:46am UTC](https://forum.juce.com/t/leaking-images/44263/15 "2021-02-15T11:46:37Z")

</div>

ah man, i think I might have stored the containing class in a static at some point - will check that… damn

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [February 15, 2021, 11:46am UTC](https://forum.juce.com/t/leaking-images/44263/16 "2021-02-15T11:46:45Z")

</div>

From [JUCE: ImageCache Class Reference](https://docs.juce.com/master/classImageCache.html#acc266a65cf61dd0a2f68e68840b4a5c4)

> “Another advantage is that after images are released, they will be kept in memory for a few seconds before it is actually deleted”

Is that what’s going on here, could you wait for a few seconds before closing your plugin to see if the leaks still occur?

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 16, 2021, 7:56am UTC](https://forum.juce.com/t/leaking-images/44263/17 "2021-02-16T07:56:03Z")

</div>

thanks @daniel - there was a static reference to the containing class which of course would be holding a reference to the images… nice one
