# Memory management guidelines

**URL:** <https://forum.juce.com/t/memory-management-guidelines/4957>\
**Category:** General JUCE discussion\
**Created:** [February 26, 2010, 8:41pm UTC](https://forum.juce.com/t/memory-management-guidelines/4957 "2010-02-26T20:41:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peteblues](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@peteblues](https://forum.juce.com/u/peteblues)\
**Post date:** [February 26, 2010, 8:41pm UTC](https://forum.juce.com/t/memory-management-guidelines/4957/1 "2010-02-26T20:41:40Z")

</div>

So, for some reason, my juce project has a lot of memory leaks when closing the app. I thought I did everything right by calling deleteAllChildren in each component’s destructor, and also calling ImageCache::release for each image I created. Now my question:  
Are there some guidelines written somewhere that I can read, about how to do memory management correctly in juce?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 26, 2010, 8:51pm UTC](https://forum.juce.com/t/memory-management-guidelines/4957/2 "2010-02-26T20:51:52Z")

</div>

I wouldn’t give any guidelines that differed from general c++ memory management… Use lots of ScopedPointers and avoid new/delete unless absolutely necessary, I guess.

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [February 27, 2010, 8:40am UTC](https://forum.juce.com/t/memory-management-guidelines/4957/3 "2010-02-27T08:40:12Z")

</div>

Tip: google for “visual leak detector”, it’s a wonderful tool (under linux, use valgrind)

---

<div class="post-metadata">

**Author:** ![peteblues](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@peteblues](https://forum.juce.com/u/peteblues)\
**Post date:** [March 5, 2010, 6:52pm UTC](https://forum.juce.com/t/memory-management-guidelines/4957/4 "2010-03-05T18:52:19Z")

</div>

I finally found my memory leak. Turned out that I was applying an image to multiple image buttons, and deleting all the buttons caused the imagecache’s refcount to go negative. Therefore, when the plugin got re-initialized (via the ‘reset default state’ in the plugindemo standalone), the image wouldn’t get created, and eventually the imagecache’s timer would delete it.  
So, I ended up having to increment the refcounter by the number of buttons that use the image-1. Is there a more elegant or automatic way of doing this? The ImageButton class automatically deletes the images attached to it (or decrements their refcounters), but it doesn’t increment the refcounter when setting images.

Another memory leak I found is that the deleteAllChildren function doesn’t delete children’s children, i.e. doesn’t delete recursively.

Peter
