# Component unit testing causes memory leaks

**URL:** <https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585>\
**Category:** General JUCE discussion\
**Created:** [January 1, 2021, 6:46pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585 "2021-01-01T18:46:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbuskov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rbuskov/32/7408_2.png) [@rbuskov](https://forum.juce.com/u/rbuskov)\
**Post date:** [January 1, 2021, 6:46pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/1 "2021-01-01T18:46:42Z")

</div>

I have some Catch2 unit tests for GUI components (derived from the juce::Component) in my project, like so:  
{  
MessageManager::getInstance(); // Force the MessageManager singleton to create an instance  
MessageManagerLock mmLock(Thread::getCurrentThread());

```
         MyGuiComponent componentUnderTest();

        // Test the component ...

        MessageManager::deleteInstance();

```

}

The tests execute just fine but for some reason the juce::Component base class causes a number of memory leaks when componentUnderTest goes out of scope:

```
*** Leaked objects detected: 1 instance(s) of class LookAndFeel_V4
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class Image
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class LookAndFeel_V2
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class LookAndFeel
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class DisplaySettingsChangeCallback
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class Desktop
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class ComponentAnimator
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class OwnedArray
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class MouseInputSource
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class MouseInputSourceInternal
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class OwnedArray
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class TypefaceCache
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 3 instance(s) of class AsyncUpdater
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 2 instance(s) of class WaitableEvent
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 1 instance(s) of class MessageManager
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
JUCE Assertion failure in juce_Singleton.h:50
JUCE Assertion failure in juce_Singleton.h:50

```

I wonder why Components don’t clean up after themselves? Perhaps they somehow share these objects with other Components behind the scenes? I have tried looking at the source code but it’s not really obvious to me what’s going on…

Finally (and most importantly), I wonder how I can avoid these memory leaks in my unit tests?

---

<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:** [January 1, 2021, 6:48pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/2 "2021-01-01T18:48:54Z")

</div>

You created an instance of the `MessageManager` but didn’t delete it.

Call `MessageManager::deleteInstance()` before your program closes.

---

<div class="post-metadata">

**Author:** ![rbuskov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rbuskov/32/7408_2.png) [@rbuskov](https://forum.juce.com/u/rbuskov)\
**Post date:** [January 1, 2021, 6:53pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/3 "2021-01-01T18:53:32Z")

</div>

I’m afraid that’s not it. Deleting the MemoryManager instance makes no difference.

I have added `MessageManager::deleteInstance();` to the example for clarity.

---

<div class="post-metadata">

**Author:** ![rbuskov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rbuskov/32/7408_2.png) [@rbuskov](https://forum.juce.com/u/rbuskov)\
**Post date:** [January 1, 2021, 7:14pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/4 "2021-01-01T19:14:30Z")

</div>

At second glance, deleting the memory manager actually _does_ make a difference… these lines no longer appear in the output when I call `MemoryManager::deleteInstance()` :

```
*** Leaked objects detected: 1 instance(s) of class MessageManager
JUCE Assertion failure in juce_LeakedObjectDetector.h:92

```

The other memory leaks remain though.

---

<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:** [January 1, 2021, 7:32pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/5 "2021-01-01T19:32:16Z")

</div>

I don’t think you want to be manually creating the `MessageManager` like that.

You should probably make a class that inherits from `JUCEApplicationBase` so it can handle the initialisation and shutting down properly.

---

<div class="post-metadata">

**Author:** ![rbuskov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rbuskov/32/7408_2.png) [@rbuskov](https://forum.juce.com/u/rbuskov)\
**Post date:** [January 1, 2021, 8:00pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/6 "2021-01-01T20:00:56Z")

</div>

OK, I solved it. Thanks to @ImJimmi for pointing me in the right direction.

Looking at the source for JuceApplicationBase, I noticed the following 2 lines of code in the `shutdown` method:

```
DeletedAtShutdown::deleteAll();
MessageManager::deleteInstance();

```

Adding these lines to my unit test solves the problem, i.e.:

```auto
{
    MessageManager::getInstance(); // Force the MessageManager singleton to create an instance
    MessageManagerLock mmLock(Thread::getCurrentThread());

    MyGuiComponent componentUnderTest();

    // Test the component ...

    DeletedAtShutdown::deleteAll();
    MessageManager::deleteInstance();
}

```

Another way to achieve this might be to create a class that derives from JuceApplicationBase, as @ImJimmi suggested.

---

<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:** [January 1, 2021, 10:12pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/7 "2021-01-01T22:12:53Z")

</div>

There is a better way, which is to put an instance of [ScopedJuceInitialiser\_GUI](https://docs.juce.com/develop/classScopedJuceInitialiser__GUI.html#details) on the stack at the beginning of your test program.  
This will create the MessageManager and what’s all necessary and when it goes out of scope, it will do the necessary cleanup. The classes affected inherit [DeletedAtShutdown](https://docs.juce.com/develop/classDeletedAtShutdown.html), if you wonder how it works.

---

<div class="post-metadata">

**Author:** ![rbuskov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rbuskov/32/7408_2.png) [@rbuskov](https://forum.juce.com/u/rbuskov)\
**Post date:** [January 2, 2021, 6:20pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/8 "2021-01-02T18:20:54Z")

</div>

@daniel nice, I’ll use that.

---

<div class="post-metadata">

**Author:** ![hexmarkrecords](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hexmarkrecords/32/11115_2.png) [@hexmarkrecords](https://forum.juce.com/u/hexmarkrecords)\
**Post date:** [August 24, 2021, 12:40pm UTC](https://forum.juce.com/t/component-unit-testing-causes-memory-leaks/43585/9 "2021-08-24T12:40:41Z")

</div>

This was helpful thanks.
