Exception thrown: read access violation. while accessing apvts values

output :

Exception thrown: read access violation. **_Scary** was 0xFFFFFFFFFFFFFFF7.

Anytime I access apvts parameters from MainPage, this exception is thrown and i cant figure out why. accessing the apvts parameters in the constructor of ProcessorEditor does not seem to be a problem, confusing me even more.
If i comment out the whole mainUIComponent then it atleast opens without crashing.

I’d really appreciate if anyone could help me, please let me now if anymore information is needed.

PluginEditor.h

class ProcessorEditor  : public juce::AudioProcessorEditor
{
public:
    ProcessorEditor (
        AudioProcessor&,
        juce::AudioProcessorValueTreeState& apvts_ref);
    ~ProcessorEditor() override;

    void paint (juce::Graphics&) override;
    void resized() override;

private:
    // This reference is provided as a quick way for your editor to
    // access the processor object that created it.
    AudioProcessor& audioProcessor;

    int settings_width_min = 200;

    std::function<void(bool)> freeze_button_callback = [](bool button_state) {
        
    };

    std::function<void(bool)> settings_button_callback = [this](bool button_state) {
        resized();
    };

    MainPage mainUIComponent;
    settingsPage settings_page_component;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProcessorEditor)
};

PluginEditor.cpp:

ProcessorEditor::ProcessorEditor(
    AudioProcessor& p,
    juce::AudioProcessorValueTreeState& apvts_ref
)
    : 
    AudioProcessorEditor(p),
    audioProcessor(p),
    mainUIComponent(apvts_ref, freeze_button_callback, settings_button_callback),
    settings_page_component(apvts_ref)
{
    setResizable(true, false);

    // This works without any problem.
    setSize (
        audioProcessor.apvts.getRawParameterValue("ui_width")->load(),
        audioProcessor.apvts.getRawParameterValue("ui_height")->load());

    DBG("-----------------------------------------------------------");
    DBG(audioProcessor.apvts.getRawParameterValue("ui_width")->load());
    DBG("-----------------------------------------------------------");

    addAndMakeVisible(mainUIComponent);
    addAndMakeVisible(settings_page_component);
}

ProcessorEditor::~AnalytiksAudioProcessorEditor()
{
}

//==============================================================================
void ProcessorEditor::paint (juce::Graphics& g)
{
    g.fillAll(juce::Colours::white);
}

void ProcessorEditor::resized()
{

    auto bounds = getLocalBounds();

    auto setting_page_width= std::max<int>(settings_width_min, bounds.getWidth() * 0.3);

    if (mainUIComponent.settings_toggle_button.state)
    {
        settings_page_component.setBounds(bounds.removeFromLeft(setting_page_width));
    }
    else 
    {
        settings_page_component.setBounds(bounds.removeFromLeft(0));
    }

    mainUIComponent.setBounds(bounds);
}

Call Stack:

 	Analytiks.exe!std::_Tree<std::_Tmap_traits<juce::StringRef,std::unique_ptr<juce::AudioProcessorValueTreeState::ParameterAdapter,std::default_delete<juce::AudioProcessorValueTreeState::ParameterAdapter>>,juce::AudioProcessorValueTreeState::StringRefLessThan,std::allocator<std::pair<juce::StringRef const ,std::unique_ptr<juce::AudioProcessorValueTreeState::ParameterAdapter,std::default_delete<juce::AudioProcessorValueTreeState::ParameterAdapter>>>>,0>>::_Find<juce::StringRef>(const juce::StringRef & _Keyval) Line 1384	C++
 	Analytiks.exe!std::_Tree<std::_Tmap_traits<juce::StringRef,std::unique_ptr<juce::AudioProcessorValueTreeState::ParameterAdapter,std::default_delete<juce::AudioProcessorValueTreeState::ParameterAdapter>>,juce::AudioProcessorValueTreeState::StringRefLessThan,std::allocator<std::pair<juce::StringRef const ,std::unique_ptr<juce::AudioProcessorValueTreeState::ParameterAdapter,std::default_delete<juce::AudioProcessorValueTreeState::ParameterAdapter>>>>,0>>::find(const juce::StringRef & _Keyval) Line 1398	C++
 	Analytiks.exe!juce::AudioProcessorValueTreeState::getParameterAdapter(juce::StringRef paramID) Line 340	C++
 	Analytiks.exe!juce::AudioProcessorValueTreeState::getRawParameterValue(juce::StringRef paramID) Line 383	C++
 	Analytiks.exe!MainPage::paint(juce::Graphics & g) Line 51	C++
 	Analytiks.exe!juce::Component::paintComponentAndChildren(juce::Graphics & g) Line 1690	C++
 	Analytiks.exe!juce::Component::paintEntireComponent(juce::Graphics & g, bool ignoreAlphaLevel) Line 1771	C++
 	Analytiks.exe!juce::Component::paintWithinParentContext(juce::Graphics & g) Line 1660	C++
 	Analytiks.exe!juce::Component::paintComponentAndChildren(juce::Graphics & g) Line 1732	C++
 	Analytiks.exe!juce::Component::paintEntireComponent(juce::Graphics & g, bool ignoreAlphaLevel) Line 1771	C++
 	Analytiks.exe!juce::Component::paintWithinParentContext(juce::Graphics & g) Line 1660	C++
 	Analytiks.exe!juce::Component::paintComponentAndChildren(juce::Graphics & g) Line 1732	C++
 	Analytiks.exe!juce::Component::paintEntireComponent(juce::Graphics & g, bool ignoreAlphaLevel) Line 1771	C++
 	Analytiks.exe!juce::Component::paintWithinParentContext(juce::Graphics & g) Line 1660	C++
 	Analytiks.exe!juce::Component::paintComponentAndChildren(juce::Graphics & g) Line 1732	C++
 	Analytiks.exe!juce::Component::paintEntireComponent(juce::Graphics & g, bool ignoreAlphaLevel) Line 1771	C++
 	Analytiks.exe!juce::ComponentPeer::handlePaint(juce::LowLevelGraphicsContext & contextToPaintTo) Line 144	C++
 	Analytiks.exe!juce::D2DRenderContext::handleDirect2DPaint() Line 5460	C++
 	Analytiks.exe!juce::D2DRenderContext::handleShowWindow() Line 5128	C++
 	Analytiks.exe!juce::HWNDComponentPeer::peerWindowProc(HWND__ * h, unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 4161	C++
 	Analytiks.exe!juce::HWNDComponentPeer::windowProc(HWND__ * h, unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 3693	C++
 	[External Code]	
 	Analytiks.exe!juce::HWNDComponentPeer::setVisible(bool shouldBeVisible) Line 1502	C++
 	Analytiks.exe!juce::Component::setVisible(bool shouldBeVisible) Line 319	C++
 	Analytiks.exe!juce::StandaloneFilterApp::initialise(const juce::String & __formal) Line 140	C++
 	Analytiks.exe!juce::JUCEApplicationBase::initialiseApp() Line 312	C++
 	Analytiks.exe!juce::JUCEApplication::initialiseApp() Line 97	C++
 	Analytiks.exe!juce::JUCEApplicationBase::main() Line 271	C++
 	Analytiks.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 234	C++
 	[External Code]	

In Plugin processor

AudioProcessor::AudioProcessor()
#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
        apvts(*this, nullptr, "PARAMETERS", create_parameter_layout())
{
}
...
juce::AudioProcessorValueTreeState::ParameterLayout AudioProcessor::create_parameter_layout()
{
    auto layout = juce::AudioProcessorValueTreeState::ParameterLayout();

    layout.add(std::make_unique<juce::AudioParameterFloat>( "ui_sep_x",   "UI Seperator X",  0.0, 1.0,    0.75 ));
    layout.add(std::make_unique<juce::AudioParameterFloat>( "ui_sep_y",   "UI Seperator Y",  0.0, 1.0,    0.6 ));

    layout.add(std::make_unique<juce::AudioParameterFloat>( "ui_width",   "Plugin Width",    150, 4000.0, 1000.0 ));
    layout.add(std::make_unique<juce::AudioParameterFloat>( "ui_height",  "Plugin Height",   150, 3000.0, 700.0));
    
    layout.add(std::make_unique<juce::AudioParameterFloat>( "ui_acc_hue", "UI Accent Hue",   0.0, 1.0,    0.9));

    return layout;
}
...

juce::AudioProcessorEditor* AudioProcessor::createEditor()
{
    return new ProcessorEditor (*this, apvts);
}

MainPage.h


class MainPage : public juce::Component
{
public:

    MainPage(
        juce::AudioProcessorValueTreeState& aptvs_ref,
        std::function<void(bool)>& freeze_button_callback,
        std::function<void(bool)>& settings_button_callback
    );

    void paint(juce::Graphics& g) override;
    void resized() override;

private:
    juce::AudioProcessorValueTreeState& apvts_ref;
    ...
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainPage)
};

MainPage.cpp

#include "MainPage.h"

MainPage::MainPage(
    juce::AudioProcessorValueTreeState& aptvs_ref, 
    std::function<void(bool)>& freeze_button_callback, 
    std::function<void(bool)>& settings_button_callback
): 
    apvts_ref(apvts_ref)
{

}

void MainPage::paint(juce::Graphics& g)
{

    // According to the callstack this is the root of the crash.
    // but accessing anything from apvts_ref in this class crashes.
    float hue = apvts_ref.getRawParameterValue("ui_acc_hue")->load();
...............
}

void MainPage::resized()
{

    if (!getParentComponent())
    {
        DBG("No parent found");
        return;
    }

    auto bounds = getLocalBounds();
    
    float v_sep_x = apvts_ref.getRawParameterValue("ui_sep_x")->load();
    float h_sep_y = apvts_ref.getRawParameterValue("ui_sep_y")->load();
    ......
}

and also from the DBG print statements “No Parent Found” (in MainPage::resized)comes before the width DBG print in the PluginEditor constructor

debug output snippet

No parent found
No parent found
-----------------------------------------------------------
1000
-----------------------------------------------------------

I can’t see any obvious problem. Does getRawParameterValue return nullptr in your paint function? What if you add some check like:

void MainPage::paint(juce::Graphics& g)
{

    // According to the callstack this is the root of the crash.
    // but accessing anything from apvts_ref in this class crashes.
    if (!apvts_ref.getRawParameterValue("ui_acc_hue")) { return; }
    float hue = apvts_ref.getRawParameterValue("ui_acc_hue")->load();
...............
}

You’re initialising apvts_ref with itself, so it’s essentially unintialised. The name of the argument is aptvs_ref. I’m surprised the compiler didn’t warn about this - I’d expect to see a “read of uninitialised data member” warning in this scenario.

3 Likes

That was the problem, fixed. Thank you.
I think it started with my typo, I do not know why the compiler did not warn me tho.