# Unique\_ptr SliderAttachment memory leak

**URL:** <https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278>\
**Category:** Audio Plugins\
**Created:** [October 16, 2021, 1:14pm UTC](https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278 "2021-10-16T13:14:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SourGummi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@SourGummi](https://forum.juce.com/u/SourGummi)\
**Post date:** [October 16, 2021, 1:14pm UTC](https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278/1 "2021-10-16T13:14:30Z")

</div>

hey, quick question.  
i’ve moved away from ScopedPointer and started using unique\_ptr, however, I’m doing something wrong because my compiler (when plugin is closed) gives message

> ‘GummiClipDistortionV0.exe’ (Win32): Unloaded ‘C:\Windows\System32\Windows.UI.dll’  
> The thread 0x3004 has exited with code 0 (0x0).  
> The thread 0x15a4 has exited with code 0 (0x0).  
> The thread 0x1578 has exited with code 0 (0x0).  
> The thread 0x7ac has exited with code 0 (0x0).  
> The thread 0x1870 has exited with code 0 (0x0).  
> The thread 0x17f8 has exited with code 0 (0x0).  
> The thread 0x3180 has exited with code 0 (0x0).  
> The thread 0x1344 has exited with code 0 (0x0).  
> Detected memory leaks!  
> Dumping objects →   
> {4967} normal block at 0x000001A5CA3B9100, 16 bytes long.  
> Data: \< P P \> 00 00 00 00 00 00 00 00 50 00 00 00 50 00 00 00  
> {4484} normal block at 0x000001A5CA3B9240, 16 bytes long.  
> Data: \< P P \> 00 00 00 00 00 00 00 00 50 00 00 00 50 00 00 00  
> Object dump complete.  
> The program ‘[12476] GummiClipDistortionV0.exe’ has exited with code 0 (0x0).

i have tested and retested and i know it’s definitely the attachment leaking. i solved the issue with slider leaking by overriding the destructor in my custom slider class.

creating

```auto
std::unique_ptr<juce::AudioProcessorValueTreeState::SliderAttachment> attachmentInputGain;
std::unique_ptr<GCD_RotarySlider> sliderInputGain;

```

constructor

```auto
sliderInputGain.reset(new GCD_RotarySlider);
addAndMakeVisible(sliderInputGain.get());
attachmentInputGain.reset(new juce::AudioProcessorValueTreeState::SliderAttachment(audioProcessor.apvts, "id_v0_input_gain", *sliderInputGain));

```

destructor

```auto
attachmentInputGain.reset(nullptr);
sliderInputGain.reset(nullptr);

```

any advice?

---

<div class="post-metadata">

**Author:** ![SourGummi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@SourGummi](https://forum.juce.com/u/SourGummi)\
**Post date:** [October 16, 2021, 3:28pm UTC](https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278/2 "2021-10-16T15:28:28Z")

</div>

i fixed my problem sort of by coincidence. After building a custom GUI and using

```auto
auto bounds = new juce::Rectangle<float>(x, y, width, height);

```

i noticed that everytime i called this object like

```auto
...bounds->getCentreX()... 

```

i received another memory leak notification.  
changing this object definition like so cleared all warnings

```auto
juce::Rectangle<float> bounds{ x, y, width, height }

```

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [October 17, 2021, 7:04pm UTC](https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278/3 "2021-10-17T19:04:20Z")

</div>

i’d like to encourage you to watch the cherno’s c++ tutorials on youtube. especially episodes that have the words “stack” and “heap” in the title. that will clear up some stuff. the cherno doesn’t talk about smart pointers in his entry videos a lot though, so let me just tell you: unique\_ptr is a wrapper around a heap allocation that makes the object it allocates dependent on the scope of the pointer. that means you don’t have to reset it in the destructor actively. whenever you can make an object in place, in stack, just do that. only if it’s not possible, use a smart pointer. an example for that would be if you have certain juce::Component that should only exist sometimes, like pop-up menus or so

---

<div class="post-metadata">

**Author:** ![SourGummi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@SourGummi](https://forum.juce.com/u/SourGummi)\
**Post date:** [October 18, 2021, 1:54pm UTC](https://forum.juce.com/t/unique-ptr-sliderattachment-memory-leak/48278/4 "2021-10-18T13:54:22Z")

</div>

Very informative thanks, I’ll be sure to check out those videos too!
