Unique_ptr SliderAttachment memory leak

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

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

constructor

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

destructor

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

any advice?

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

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

i noticed that everytime i called this object like

...bounds->getCentreX()... 

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

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

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

1 Like

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