Plugin won't open after creating APVTS slider attachment

Hi, I’m new to JUCE and have been trying to implement an automation parameter to a gain slider, following a tutorial from The Audio Programmer. Ableton does show the parameter.

However, creating a SliderAttachment prevents the VST from opening at all. VS throws no errors so I can’t tell where the issue is. I’ve been trying to debug for about 5 hours and can’t find any similar posts to help.

Here is some code in PluginEditor.cpp:


SimpleGainSliderAudioProcessorEditor::SimpleGainSliderAudioProcessorEditor (SimpleGainSliderAudioProcessor& p)
    : AudioProcessorEditor (&p), audioProcessor (p)
{
    // Make sure that before the constructor has finished, you've set the
    // editor's size to whatever you need it to be.
    setSize (200, 400);


    
    gainSlider.setSliderStyle(juce::Slider::SliderStyle::LinearVertical);
    gainSlider.setTextBoxStyle(juce::Slider::TextBoxBelow, true, 100, 25);
    gainSlider.setRange(-48.0, 0.0);
    gainSlider.setValue(-1.0);
    
    gainSlider.addListener(this);

    addAndMakeVisible(gainSlider);

    //create sliderAttachment linking processors's treeState to gainSlider

    sliderAttach = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(audioProcessor.treeState, GAIN_ID, gainSlider);
}

Let me know if there is other code you need to check.

Commenting out the line where sliderAttach is declared fixes the crash, but I can’t figure out what is wrong with this line. VS throws no errors.

Help is appreciated, over the last 5 hours I’ve tried about every avenue there is other than creating a forum post.

If you can comment out the slider attachment and it works, I would verify if the parameter exists. Add this line before creating the attachment:

jassert (audioProcessor.treeState.getParameter (GAIN_ID));

In the debugger this will halt execution if the parameter doesn’t exist.

N.B. you can simplify your code by a) setting up the sliderStyle in the header and b) setRange and setValue will be overwritten by the attachment, so you can remove those lines as well:

juce::Slider gainSlider { juce::Slider::SliderStyle::LinearVertical, juce::Slider::TextBoxBelow };

Only if the TextBox position options (not the below) was not the default, you might have to change that manually like you did.

Thank you for your advice.

Adding the jassert before creating sliderAttach doesn’t trigger the breakpoint.

Debugging line by line, eventually the program fails inside a VS C library trying to make sliderAttach:

Is it to do with null pointers or datatypes or something? This is the first time I’m trying to implement parameters and I can’t understand why it’s being so difficult.

This is the call stack for the error:

simpleGainSlider.exe!std::_Func_class<juce::String,float>::operator()(float <_Args_0>) Line 917
	at C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\functional(917)

Before this happens, I can see this, which looks worrying but I’m not exactly sure what to make of it:

The call stack should be a list of functions, not just one position.
The error is indeed way to far inside the details, so it is hard to figure out what went wrong.

Click the call stack one by one out of the stack (I always hum Talking Heads when debugging: “how did I get here”).

Things to look for:

  • is this a legit address?
  • is there a mathematical problem?
  • what memory do I access, is that legit?

When you are stuck, best post the whole call stack with line numbers

Thank you for your reply. The call stack is mostly library code and pointer values I can’t make sense of, but none of it had red flags.

Can you check if it is to do with my implementation of the treeState in the processor constructor?

SimpleGainSliderAudioProcessor::SimpleGainSliderAudioProcessor():
#ifndef JucePlugin_PreferredChannelConfigurations
      AudioProcessor (BusesProperties()
                     #if ! JucePlugin_IsMidiEffect
                      #if ! JucePlugin_IsSynth
                       .withInput  ("Input",  juce::AudioChannelSet::stereo(), true)
                      #endif
                       .withOutput ("Output", juce::AudioChannelSet::stereo(), true)
                     #endif
                       ),
    

#endif
    //initialisation list
    treeState(*this, nullptr),
    gainValue(-1.0)
    

{   //processor construtor code
    
    juce::NormalisableRange<float> gainRange (-48.0f, 0.0f);
    treeState.createAndAddParameter("gain", GAIN_NAME, GAIN_NAME, gainRange, -12.0f, nullptr, nullptr);
    treeState.state = juce::ValueTree("gain");
}

Solved.

treeState.createAndAddParameter() is deprecated and parameters shouldn’t be added this way. I knew this but assumed it should still be functional. Don’t use this method or any tutorials that use it. Parameters are now declared within the APVTS constructor.