# Static image memory leak

**URL:** <https://forum.juce.com/t/static-image-memory-leak/41007>\
**Category:** General JUCE discussion\
**Created:** [July 30, 2020, 3:18pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007 "2020-07-30T15:18:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [July 30, 2020, 3:18pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/1 "2020-07-30T15:18:02Z")

</div>

It seems I just can’t figure this one on my own.

The following code generates a memory leak, but why?

```
class pngHandler
{

public:    

     static pngHandler& Get()
     {
         static pngHandler instance;
         return instance;
     }
 
	  void InitGUI() { 
      knobImage = juce::PNGImageFormat::loadFrom(juce::File("some_file.png"));	
     }

private:
	juce::Image knobImage;	
	pngHandler() {}

};

```

In MainComponent’s constructor:

`pngHandler::Get().InitGUI();`

Once the main GUI window is terminated, an assertion is raised saying that a `SoftwarePixelData` object is leaking.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 30, 2020, 3:22pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/2 "2020-07-30T15:22:23Z")

</div>

It’s because of the order the destructors of the objects are run when shutting down. The Juce memory leak detector is destroyed first and it notices the Image hasn’t yet been destroyed and triggers the memory leak warning.

Why do you need to have the Image as a static object?

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [July 30, 2020, 3:29pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/3 "2020-07-30T15:29:33Z")

</div>

pngHandler does a lot of stuff and handles every component in the app. I just want to be able to access it from any class so I made it a singleton, where I can call it from anywhere with just one line. Is it bad to have a static image? (This particular image should also be passed to multiple knobs to serve as a background).

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 30, 2020, 3:33pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/4 "2020-07-30T15:33:28Z")

</div>

It’s bad to have static variables to begin with, you are going to end up having those Juce memory leak warnings for other Juce objects too, not just Images.

Juce does however have some helper utilities for dealing with singletons like the JUCE\_DECLARE\_SINGLETON macro. I have not myself used that because I want to keep my code compatible for use in plugins.

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [July 30, 2020, 3:39pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/5 "2020-07-30T15:39:22Z")

</div>

Noted, thanks.

---

<div class="post-metadata">

**Author:** ![HowardAntares](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/howardantares/32/4629_2.png) [@HowardAntares](https://forum.juce.com/u/HowardAntares)\
**Post date:** [July 30, 2020, 3:52pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/6 "2020-07-30T15:52:58Z")

</div>

> [@duvrin](#):
>
> I just want to be able to access it from any class so I made it a singleton, where I can call it from anywhere with just one line.

That’s not a good reason to have a singleton. All you have to do is provide a getter() in the owner of the pngHandler to get a reference to that object, and use that to access the pngHandler object.

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [July 30, 2020, 5:10pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/7 "2020-07-30T17:10:33Z")

</div>

The essence of pngHanlder is that there should be only one of it to handle the app’s boiler plate stuff, and every component from the top level to the last child should be able to access it directly in order to feed some data into it. Considering this, isn’t a singleton a right choice?

---

<div class="post-metadata">

**Author:** ![HowardAntares](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/howardantares/32/4629_2.png) [@HowardAntares](https://forum.juce.com/u/HowardAntares)\
**Post date:** [July 30, 2020, 5:28pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/8 "2020-07-30T17:28:56Z")

</div>

No. Why? Make it a member of the editor itself, and let the components ask the editor for a reference to it. That gives you a single instance, because there is only one editor.

---

<div class="post-metadata">

**Author:** ![dave-elton](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave-elton/32/7869_2.png) [@dave-elton](https://forum.juce.com/u/dave-elton)\
**Post date:** [July 30, 2020, 9:19pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/9 "2020-07-30T21:19:10Z")

</div>

Noting that you say App rather than plugin, and assuming you use the projucer app/GUI template you could create your pngHandler as a member of the your class that extends juce::JUCEApplication and pass a ref into the MainWindow and so on.

My apps and more complex plugins all have a ‘context’ class that is effectively a bag of such common systems and it gets passed by reference around the UI mostly to minimise the amount of constructor params whilst avoiding global statics but also to keep the ‘business logic’ separate from the more transient UI.

HTH,  
Dave

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [July 30, 2020, 9:29pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/10 "2020-07-30T21:29:34Z")

</div>

And, to follow on what @dave-elton says. depending on the data you want to store and access, I use a ValueTree based system, where I pass in PersistantProperties and RuntimeProperties ValueTrees to all Components, which can then use a set of helper classes/wrappers to find and use specific data from those ValueTrees

---

<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:** [August 3, 2020, 9:59pm UTC](https://forum.juce.com/t/static-image-memory-leak/41007/11 "2020-08-03T21:59:24Z")

</div>

In case you missed it, there is a good solution for image assets, the ImageCache. Images are loaded only once, and the pixel data is already shared, so no copying and no extra loading takes place. And the ImageCache has static methods to access the images, no need to pass around pointers or references.
