# How to construct AudioProcessorValueTreeState::SliderAttachment with std::unique\_ptr\<juce::Slider\>

**URL:** <https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038>\
**Category:** The Projucer\
**Created:** [August 1, 2020, 8:55pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038 "2020-08-01T20:55:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 1, 2020, 8:55pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/1 "2020-08-01T20:55:43Z")

</div>

Hello,

I have only recently started implementing in Projucer / Juce.

I have one problem that I’m trying to solve:  
1- I use the GUI Editor in Projucer to create knobs and sliders  
2- I want to use AudioProcessorValueTreeState::SliderAttachment for the parameters.

The problem is that Projucer GUI Editor creates unique pointers such as

```
std::unique_ptr<juce::Slider> m_sineSlider;

```

which SliderAttachment constructor does not accept since it is expecting a reference to a slider:

```
new AudioProcessorValueTreeState::SliderAttachment(m_processor.m_treestate, m_sineSlider->getName(), m_sineSlider); // Does not compile 

```

Since last parameter of the constructor is pass by reference:  
**SliderAttachment** (  
AudioProcessorValueTreeState& stateToUse,  
const String& parameterID,  
**Slider& slider** );

I prefer not to go back to manually generating UI so I’m wondering if there’s a way to do this?

Thank you.

Cheers,  
Aram

---

<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:** [August 1, 2020, 9:17pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/2 "2020-08-01T21:17:31Z")

</div>

To get a reference to the unique\_ptr you simply dereference it adding an asterisk ˋ\*m\_sineSliderˋ

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [August 1, 2020, 9:47pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/3 "2020-08-01T21:47:01Z")

</div>

@aram depending on your needs, you may be quite restricted in what you can achieve with the Projucer layout tools. If you want to do anything of complexity you should develop your skill set in manual layout… or, use more powerful like [Plugin Gui Magic](https://foleysfinest.com/developer/pluginguimagic/)

---

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 1, 2020, 9:48pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/4 "2020-08-01T21:48:21Z")

</div>

Thank you Daniel for the suggestion.

I have already tried that but the application crashes upon closing in juce::HeapBlock and that’s why I thought to ask how anyone is using the Projucer GUI Editor with parameter support.

---

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 1, 2020, 9:53pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/5 "2020-08-01T21:53:27Z")

</div>

Thanks for the comment @cpr2323 . I don’t think that manual creation of gui is the better equivalent of WYSIWYG.

Plugin Gui Magic looks very appealing, however it costs.

I am certain there must be a way to use Projucer GUI Editor and properly generate parameters without issues. I’m hoping someone on the forum has done this before.

I spent most of the 2 3 days manually creating knobs and sliders, creating labels, images etc before posting this so I really am looking for the right way to make GUI, using GUI.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [August 1, 2020, 10:12pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/6 "2020-08-01T22:12:39Z")

</div>

I didn’t mean to imply that manual is better than a WYSIWYG editor, only that the Projucer editor is old and crufty, without much new work having gone into for a while. At one point (not sure if this is still the case, it was considered `deprecated`) I would hate for you to get frustrated trying to do creative things that the editor can’t do for you. although, a quick search does how there are people who are happy with it, so my comments are likely biased by my own preference. 🙂 And I am sure someone who is using it could answer your actual question. Are you also learning C++ while you learn JUCE?

---

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 1, 2020, 10:19pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/7 "2020-08-01T22:19:38Z")

</div>

It’s all good @cpr2323 🙂 The fact that I received responses under an hour from JUCE community is a gem itself. I use C++ on daily basis.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [August 1, 2020, 11:35pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/8 "2020-08-01T23:35:54Z")

</div>

> [@aram](#):
>
> I have already tried that but the application crashes upon closing in juce::HeapBlock and that’s why I thought to ask how anyone is using the Projucer GUI Editor with parameter support.

Dereferencing the unique pointer should not be the reason for that. I suspect that the order of destruction is what could be problematic here, if you declare your attachment as a member before the slider itself or if you explicitly reset the slider member before destructing the attachment this can lead to an attachment calling a slider it references to that no longer exists. To make things easier, I created a helper class template for my projects that groups a widget with the matching attachment (the matching attachment type is selected automatically through `std::enable_if`) so that you can just write e.g.

```auto
jb::AttachedWidget<Slider> m_sineSlider { apvts, sineSliderID, /* slider constructor args here * /} 

```

This makes sure that the destruction order is always right. You can find it here:

> <https://github.com/JanosGit/PluginBase/blob/master/jb_plugin_base/Parameters/AttachedWidget.h>

However, this is not exactly matching the auto generated gui editor code. But as others said above, I’d advise you to try to avoid it as it is not supported anymore and is quite unflexible. Building GUIs from code is much more powerful at least with the current state of JUCE and what most pro users I know do. If you use JUCE\_LIVE\_CONSTANT polled in a timer to set sizes and positions it’s not so hard to tweak parts of the GUI at runtime 🙂

---

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 2, 2020, 1:33am UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/9 "2020-08-02T01:33:17Z")

</div>

Thank you @PluginPenguin for the explanation!

One of the GUI Editor generated limitations is that there is only a limited number of locations things can be written (ie, the bracketed code is before the auto generated code etc) and makes it harder to play around the orders. As you mentioned, some listeners are being destroyed before the attachments are done with them.

I think I’ll take another direction that still may be good enough:

- Use gui and generated code
- Move the generated code to bracketed area
- Delete gui that removes the original code
- Change the content as needed.

I might also look into a Xaml to Code I wrote a while back or something similar to help make the boilerplate parts faster.

By the way I noticed you have a Preset Manager in that github, I hope you don’t mind me looking around that as I noticed that’s another thing missing from the Juce framework.

Cheers!

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [August 2, 2020, 7:54am UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/10 "2020-08-02T07:54:16Z")

</div>

> [@aram](#):
>
> By the way I noticed you have a Preset Manager in that github, I hope you don’t mind me looking around that as I noticed that’s another thing missing from the Juce framework.

Sure, look around as much as you want and use everything you find there according to the licenses the individual projects are released under if you want to 🙂

If you are interested in the preset manager, you might also want to check out my `PluginAudioProcessorBase` class that avoids a lot of boilerplate code for my products, by setting up an `AudioProcessor` along with the preset manager and an apvts and overrides several member functions in a senseful way… example usage of all this can be found here [GitHub - JanosGit/Schrammel\_OJD at develop](https://github.com/JanosGit/Schrammel_OJD/tree/develop)

---

<div class="post-metadata">

**Author:** ![aram](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aram/32/9191_2.png) [@aram](https://forum.juce.com/u/aram)\
**Post date:** [August 2, 2020, 2:23pm UTC](https://forum.juce.com/t/how-to-construct-audioprocessorvaluetreestate-sliderattachment-with-std-unique-ptr-juce-slider/41038/11 "2020-08-02T14:23:02Z")

</div>

Thank you, will do!
