# Debug leaked objects

**URL:** <https://forum.juce.com/t/debug-leaked-objects/47658>\
**Category:** Development\
**Created:** [September 3, 2021, 11:21am UTC](https://forum.juce.com/t/debug-leaked-objects/47658 "2021-09-03T11:21:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacro](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dacro](https://forum.juce.com/u/dacro)\
**Post date:** [September 3, 2021, 11:21am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/1 "2021-09-03T11:21:09Z")

</div>

Hey JUCE community,

when closing our Mac App. the app can not be closed correctly:

Thread:  
JUCE Message Thread (1) Queue : com.apple.main-thread (serial)

in  
#0 0x000000010c714b61 in juce::LeakedObjectDetectorjuce::ValueTree::SharedObject::LeakCounter::~LeakCounter() at /Users/……/juce\_LeakedObjectDetector.h:92

```auto
*** Leaked objects detected: 16 instance(s) of class SharedObject
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 2 instance(s) of class DynamicObject
JUCE Assertion failure in juce_LeakedObjectDetector.h:92

```

We had some similar errors and fixed them because we could find the class which does not leaked objects. But with these errors we can not find the class. Is there any tool to find the leaked objects? I tried [https://github.com/jcredland/juce-toys/tree/master/jcf\_advanced\_leak\_detector](https://github.com/jcredland/juce-toys/tree/master/jcf_advanced_leak_detector) but the detector does not print anything, even when I continue the debugger after JUCE\_LEAK\_DETECTOR assert fires. I initialize “jcf::AdvancedLeakDetector advancedLeakDetector;” in multiple classes, but still no print.

Any other tools or procedures to find the leak?

Thanks  
David

---

<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:** [September 3, 2021, 11:30am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/2 "2021-09-03T11:30:33Z")

</div>

Make sure to add the `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyClass)` or `JUCE_LEAK_DETECTOR (MyClass)` to all your own classes, so they can be detected as well.

That helps you to find the enclosing class that is leaking (assuming it is one of yours, but that’s most likely given that this is the only code nobody tests besides yourself)

Good luck

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [September 3, 2021, 11:57am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/3 "2021-09-03T11:57:40Z")

</div>

There is also `JUCE_HEAVYWEIGHT_LEAK_DETECTOR`.

You can add it to the definition of the leaked object’s class e.g. `SharedObject`. In this case you will have to replace the `JUCE_LEAK_DETECTOR` macro with the heavyweight version.

On top of detecting the leak this will also print a stack trace to help finding where the allocation happened.

Once the leak has been fixed you should switch back to the simple leak detector though, as saving the stack trace for every allocation is pretty expensive.

---

<div class="post-metadata">

**Author:** ![dacro](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dacro](https://forum.juce.com/u/dacro)\
**Post date:** [September 9, 2021, 10:44am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/4 "2021-09-09T10:44:54Z")

</div>

Thanks! The `JUCE_HEAVYWEIGHT_LEAK_DETECTOR is awesome!`

---

<div class="post-metadata">

**Author:** ![andym-2](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@andym-2](https://forum.juce.com/u/andym-2)\
**Post date:** [May 12, 2022, 11:52am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/5 "2022-05-12T11:52:20Z")

</div>

I’m having this problem on Monterey M1 desktop app, I have `JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyClass)` in all of my classes but get:

\*\*\* Leaked objects detected: 328 instance(s) of class SharedObject  
JUCE Assertion failure in juce\_LeakedObjectDetector.h:92  
(lldb)

I’ve scoured my code, I am not using new/delete anywhere and using unique\_ptr and shared\_ptr for all dynamic objects. The only exception is FileBrowserComponent which I can’t make work with unique\_ptr. however I’ve compiled with that commented out and it isn’t the problem.

Presumably SharedObject is just a generic warning, so without knowing the offending class name I guess `JUCE_HEAVYWEIGHT_LEAK_DETECTOR` won’t help me?

Wondering if anyone has any tips on dealing with this situation?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [May 12, 2022, 4:01pm UTC](https://forum.juce.com/t/debug-leaked-objects/47658/6 "2022-05-12T16:01:34Z")

</div>

SharedObject is used to implement ValueTree, so I assume a ValueTree is being leaked somehow. You could try adding a heavyweight leak detector to the definition of ValueTree and see whether it gives you any useful info.

---

<div class="post-metadata">

**Author:** ![andym-2](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@andym-2](https://forum.juce.com/u/andym-2)\
**Post date:** [May 13, 2022, 9:38am UTC](https://forum.juce.com/t/debug-leaked-objects/47658/7 "2022-05-13T09:38:58Z")

</div>

Thanks very much, this narrowed down the problem and it was down to Valuetree and scope, it’s a massive relief to have it solved! On the plus side it made me really tighten up my code whilst hunting it down.
