# Leak detector and static variables

**URL:** <https://forum.juce.com/t/leak-detector-and-static-variables/12721>\
**Category:** General JUCE discussion\
**Created:** [April 18, 2014, 10:53am UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721 "2014-04-18T10:53:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![joanpuigsanz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/joanpuigsanz/32/183_2.png) [@joanpuigsanz](https://forum.juce.com/u/joanpuigsanz)\
**Post date:** [April 18, 2014, 10:53am UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/1 "2014-04-18T10:53:08Z")

</div>

Hi there,

I'm using the macro&nbsp;JUCE\_DECLARE\_NON\_COPYABLE\_WITH\_LEAK\_DETECTOR in a class that I instantiate using a static, the class is destroyed properly but the leak detector is always telling me that there is a leak which it is not true. I would like to cover my back using the leak detector macro but I have this issue, how can I fix it?

Thanks!

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [April 18, 2014, 1:58pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/2 "2014-04-18T13:58:00Z")

</div>

For instance:

1. Use&nbsp;Singleton with&nbsp;DeletedAtShutdown inheritance.

2. Use SharedResourcePointer.

3. Create a dummy object&nbsp;to&nbsp;reverse the order of the static initializers.

---

<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:** [April 18, 2014, 2:17pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/3 "2014-04-18T14:17:02Z")

</div>

And use function-scoped statics, not file-scoped statics.

---

<div class="post-metadata">

**Author:** ![joanpuigsanz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/joanpuigsanz/32/183_2.png) [@joanpuigsanz](https://forum.juce.com/u/joanpuigsanz)\
**Post date:** [April 18, 2014, 3:41pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/4 "2014-04-18T15:41:39Z")

</div>

1. As I'm building a library with no UI I can not use&nbsp;DeletedAtShutdown

2. It is not very clear to me how to use&nbsp;SharedResourcePointer with static fields, can anybody write an example?

3. &nbsp;How can I create&nbsp;a dummy object&nbsp;to&nbsp;reverse the order of the static initializers? any example?

Thanks!

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [April 19, 2014, 5:34am UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/5 "2014-04-19T05:34:00Z")

</div>

AFAIK.

The leak detector of a class FOO is based on a local static variable and is built at the first instantiation of a FOO object. So according to C++ standard every static object constructed before&nbsp;will be released after (in reverse order). That's why you get false-positives. When the leak detector is destroyed (as Foo objects could still be owned by those oldest static objects) leaks are reported.&nbsp;

Note that "constructed" means totally constructed. If a static A calls a local static B in its constructor, B will be considered constructed before A. So B will be released after A.&nbsp;Note that "local static" means "function&nbsp;static" and "non-local" static means "global static".&nbsp;

I'm not english speaking native. So please correct me if something is wrong.

Various approaches could be used to turn off LEAK\_DETECTOR hell, but it really depends on situations.&nbsp;

0.&nbsp;Do NOT use LEAK\_DETECTOR in that case.

1. You could use an Initialize/Shutdown instead (as JUCE does) to explicitly manage order of construction.

2. SharedResourcePointer example in the documentation isn't good enough?

3. Example:

&nbsp; &nbsp;- Static class FOO is constructed.

&nbsp; &nbsp;- Static class BAR is constructed (with LEAK\_DETECTOR).

&nbsp; &nbsp;- A BAR instance&nbsp;is added to (owned by) the FOO container.&nbsp;

&nbsp; &nbsp;- Shutdown.

&nbsp; &nbsp;- BAR's LEAK\_DETECTOR destructor is called : Warning! There's still a BAR instance in FOO!

&nbsp; &nbsp;...

&nbsp; &nbsp;Solution: Instantiate a dummy BAR before FOO. It can be in the FOO constructor ;-)

```
class Foo {
    public:
        Foo( ) { Bar("Dummy"); }
};

class Bar {
    public:
        Bar(const String& hello) { DBG(hello); }

    private:
        JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Bar)
};

static Foo container;
```

&nbsp; &nbsp;...

&nbsp; &nbsp;Now:

&nbsp; &nbsp;- Static class FOO constructor is called.

&nbsp; &nbsp;- The static&nbsp;BAR's LEAK\_DETECTOR is constructed.

&nbsp; &nbsp;- Static class FOO is constructed.

&nbsp; &nbsp;- A BAR instance&nbsp;is added to (owned by)&nbsp;the FOO container.

&nbsp; &nbsp;- Shutdown.

&nbsp; &nbsp;- FOO destructor is called.

&nbsp; &nbsp;- BAR's LEAK\_DETECTOR destructor is called.&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![joanpuigsanz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/joanpuigsanz/32/183_2.png) [@joanpuigsanz](https://forum.juce.com/u/joanpuigsanz)\
**Post date:** [April 22, 2014, 12:05pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/6 "2014-04-22T12:05:52Z")

</div>

Thanks, now is clear! I fixed the issue with the leak detector :)

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [April 22, 2014, 1:25pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/7 "2014-04-22T13:25:01Z")

</div>

You're welcomed.

References for future readers:&nbsp;

- http://stackoverflow.com/a/3737070

- http://isocpp.org/files/papers/N3690.pdf

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Perez](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nathaniel_perez/32/1009_2.png) [@Nathaniel\_Perez](https://forum.juce.com/u/Nathaniel_Perez)\
**Post date:** [September 8, 2016, 10:11am UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/8 "2016-09-08T10:11:22Z")

</div>

Hi

though i understood the principle of the solution, I could not figure out how to implement this, since I am using here JUCE classes, that I am not willing to mofify.

To be clearer, here is part of my code:

Let’s say MyClass is a user defined class:

```
class MyClass
{
// some code

    static AudioDeviceManager& getDeviceManager()
    {
        static AudioDeviceManager deviceManager;

        return deviceManager;
    }
};

```

How can I use the dummy thing in my case?

Thank you  
Nathaniel

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [September 8, 2016, 4:26pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/9 "2016-09-08T16:26:26Z")

</div>

The dummy trick above is not relevant here (anyway IMHO it is never an elegant solution).  
I’m not used with the AudioDeviceManager class.  
In your code the AudioDeviceManager is locally static ;  
I suppose thus that its leak detector is constructed last.  
Is there any static class that owns an instance of it and that is constructed before?

---

<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:** [April 22, 2023, 12:54pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/10 "2023-04-22T12:54:18Z")

</div>

Have spent an embarrassingly long time debugging a detected leak to find it’s just this - the object with the leak detector was being constructed after the static container it was added to.

No fix for this on the JUCE side AFAIK, but it would be nice if the needlessly-passive-aggressive comment above the assertion in `juce_LeakedObjectDetector.h` could be updated to mention something about this edge case with statics:

> <https://github.com/juce-framework/JUCE/blob/69795dc8e589a9eb5df251b6dd994859bf7b3fab/modules/juce_core/memory/juce_LeakedObjectDetector.h#L92>

---

<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:** [April 22, 2023, 12:55pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/11 "2023-04-22T12:55:13Z")

</div>

Super simple repro if it’s at all helpful:

```cpp
struct NoLeak
{
    NoLeak() { DBG("Constructed"); }
    ~NoLeak() { DBG("Destructed"); }

    JUCE_LEAK_DETECTOR(NoLeak)
};

int main()
{
    static std::unique_ptr<NoLeak> noLeak;
    noLeak = std::make_unique<NoLeak>();

    return 0;
}

```

```plaintext
JUCE v7.0.5
Constructed
*** Leaked objects detected: 1 instance(s) of class NoLeak
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
Destructed

```

---

<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:** [April 22, 2023, 6:45pm UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/12 "2023-04-22T18:45:54Z")

</div>

If you don’t want a leak detected, just don’t add the leak detector.

A better way is to use a singleton like mentioned before. And to have your class properly cleaned up, you can have a look at the [DeletedAtShutdown](https://docs.juce.com/master/classDeletedAtShutdown.html#details) class.

---

<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:** [April 23, 2023, 9:01am UTC](https://forum.juce.com/t/leak-detector-and-static-variables/12721/14 "2023-04-23T09:01:43Z")

</div>

Neither of those are an option for my case since the class in question is a fairly generic data structure of which I’d expect to have many instances in an applicaiton, and it inherits from a JUCE class that itself has a leak detector, so I’d just get less helpful warnings instead.
