Memory management guidelines


#1

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?


#2

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.


#3

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


#4

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