# Should we use JUCE\_LEAK\_DETECTOR in base classes?

**URL:** <https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604>\
**Category:** General JUCE discussion\
**Created:** [March 1, 2018, 5:28am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604 "2018-03-01T05:28:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [March 1, 2018, 5:28am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/1 "2018-03-01T05:28:53Z")

</div>

I’ve been chasing a brain-melting, keyboard smashing bug for the last three days where the leak detector has been firing off in one class no matter what I do - even if I just do a local allocation on the stack, as soon as the object goes out of scope I get a dangling pointer leaked object assertion claiming I’m trying to double `delete` which obviously makes no sense.

The base class uses the `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR` macro, as does the derived class. Looking through JUCE code, I couldn’t find any instances of base classes using this macro. I recognize the overhead + redundancy of having two instances of the leak detector in the class hierarchy, but is it actually _harmful_ to do so? If so, can we get some documentation on it so nobody else has to endure the debugging suffering I have experienced?

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [March 1, 2018, 7:40am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/2 "2018-03-01T07:40:08Z")

</div>

Component uses `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR` as do the derived classes such as `Slider`. Could it be something like having virtual methods without a virtual destructor in the base class? I’m not sure why it would think you had deleted it twice though. Also is it possible to accidently pass the same value to both instances of `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR`

I’m not sure this would even compile, but maybe something like…

```
class Base
{
    //... stuff goes here
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Base)
}

class Derived : public Base
{
    //... stuff goes here
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Base) // this should be Derived not Base
}
```

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [March 1, 2018, 7:51am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/3 "2018-03-01T07:51:23Z")

</div>

OK I’ve just checked and sure enough I think it’s likely that your Base class doesn’t have a virtual destructor. For example this causes a leak.

```
struct Base
{
    Base() {}
    ~Base() {} // add virtual keyword here to stop the leak
    virtual void someMethod() = 0;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Base)
};

struct Derived : public Base
{
    Derived() {}
    ~Derived() {}
    void someMethod() override {}
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Derived)
};

// do this in the code somewhere
ScopedPointer<Base> obj = new Derived();
```

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [March 1, 2018, 8:26am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/4 "2018-03-01T08:26:53Z")

</div>

Thanks for thinking about this and sharing ideas. My destructor is virtual, and the specified classes in the leak detector macro are correct. Great point on `Component` being a base class which contains the leak detector.

I legitimately _cannot_ pinpoint what is causing this. As a workaround I’m not using the leak detector macro in my base class and only in the derived classes - that seems to work it out, hack as it may be.

In an _extremely_ strange twist, I made a direct copy of the class header/implementation file, renamed it to something similar, and ran my same tests - the copied class doesn’t produce leak errors, but the original class does. I quadruple checked there were no conflicting/duplicate symbols, nuked the build folders + derived data, nothing seems to produce logic.

For some insight into the code itself, you can check it out (rev. 93062f6). The base class is `app/Source/hmnz_ValueTreeObject.h`, the derived class is `app/Source/hmnz_Automation.h`. Setting a breakpoint in `app/Source/hmnz_AutomationUnitTest.h` should allow you to step through instantiation and deletion - that’s where it’s blowing up. FWIW this is under clang, and the `.jucer` project should compile out of the box on macOS.

> **[jonathonracz/harmonaize](https://github.com/jonathonracz/harmonaize)**
>
> An undergraduate senior project DAW featuring accompaniment generation. - jonathonracz/harmonaize

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [March 1, 2018, 8:37am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/5 "2018-03-01T08:37:17Z")

</div>

The plot thickens. After removing the base class leak detector I get a crash in the actual app when beginning an undo transaction, with the debugger complaining about an access violation on a const string. [According to](https://forum.juce.com/t/stringholder-crashes/8696) @jules this is likely because of a dangling pointer, which is believed to be the culprit already, so I guess there is in fact some kind of problem with the code…?

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [March 1, 2018, 10:23am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/6 "2018-03-01T10:23:20Z")

</div>

Hmmm I can’t replicate anything in my simple example but I wonder if this _might_ have something to do with a mixing of templates and virtual methods? Certainly seems odd though.

---

<div class="post-metadata">

**Author:** ![rdthompson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rdthompson/32/5476_2.png) [@rdthompson](https://forum.juce.com/u/rdthompson)\
**Post date:** [July 27, 2019, 8:56am UTC](https://forum.juce.com/t/should-we-use-juce-leak-detector-in-base-classes/26604/7 "2019-07-27T08:56:39Z")

</div>

For details on leaks, I was directed towards jcf\_advanced\_leak\_detector.h found at, jcredland’s [**juce-toys**](https://github.com/jcredland/juce-toys/tree/7701ad96c037454031be1d6895f48aa0635bbd84/jcf_advanced_leak_detector)

However, some guidance would be appreciated. I had it working once, then suddenly on rebuilding a project I had some strange errors.

Has anyone used these excellent diagnostic tools for ensuring a robust JUCE App?  
Thanks in Advance.
