# EXC\_BAD\_ACCESS on child components creation

**URL:** <https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910>\
**Category:** General JUCE discussion\
**Created:** [July 12, 2021, 7:53pm UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910 "2021-07-12T19:53:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandoval31](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sandoval31](https://forum.juce.com/u/sandoval31)\
**Post date:** [July 12, 2021, 7:53pm UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910/1 "2021-07-12T19:53:36Z")

</div>

Hi there,  
I cant’ figure out what is wrong with my code. I’ve code parents and child components with sliders and labels. More precisely, I’ve got a struct FlapPanel and a struct MotorPanel in my struct ButtonsPanel. There all inherit from component.  
I’ve got an EXC\_BAD\_ACCESS error with this code (in juce\_Memory.h /inline Type readUnaligned ).

The strange think is that it work perfectly if I keep only one of the two child components, or if I remove some sliders or labels. However, I can’t find how to make it work with all the components.

Any idea?  
Please find below my code (sorry about the size…)  
Thanks a lot for your help!

In the **.h** :

```
struct MotorPanel : public Component
{
    MotorPanel();
    void resized() override;
    void paint (Graphics& g) override;
    void setSIO(SiO * s);

    SIO * sIO;

    juce::Label motorLabel; 
    juce::Slider vibratoSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    juce::Label vibratoSliderLabel { {}, "Vibrato" };

    juce::Slider vibrato25Slider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    juce::Label vibrato25SliderLabel { {}, "Vibrato + 25V" };

    juce::Slider speedSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    juce::Label speedSliderLabel { {}, "Speed" };

    juce::Slider depthSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    juce::Label depthSliderLabel { {}, "Depth" };

    Slider accSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label accSliderLabel { {}, "Acceleration" };

};

struct FlapPanel : public Component
{
    FlapPanel();   
    void resized() override;
    void paint (Graphics& g) override;
    void setSIO(SIO * s);

    SIO * sIO;
    juce::Label tremoloLabel;    
    Slider flapReleaseSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label flapReleaseSliderLabel { {}, "Release" };

    Slider flapAttackSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label flapAttackSliderLabel { {}, "Attack" };

    Slider flapSpeedSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label flapSpeedSliderLabel { {}, "Speed" };

    Slider flapDepthSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label flapDepthSliderLabel { {}, "Depth" };
};

struct ButtonsPanel : public Component
{
    ButtonsPanel();
    void resized() override;    
    void paint (Graphics& g) override;
    void setSIO(SIO * s);

    Slider volumeSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label volumeSliderLabel { {}, "Volume" };
    juce::TextButton resetButton;
    Slider pitchbendSlider{ Slider::RotaryHorizontalVerticalDrag, Slider::NoTextBox};
    Label pitchbendSliderLabel { {}, "PitchBend" };

    SIO * sIO;
    FlapPanel flapPanel;
    MotorPanel motorPanel;
};

```

And in the **.cpp** :

```
MotorPanel::MotorPanel()
{
    addAndMakeVisible (motorLabel);
    motorLabel.setText ("Motor", dontSendNotification);
    motorLabel.setJustificationType(juce::Justification::centred);
    motorLabel.setFont(22);

    addAndMakeVisible (speedSliderLabel);
    speedSliderLabel.attachToComponent(&speedSlider, false);
    speedSliderLabel.setJustificationType(juce::Justification::centred);
    speedSlider.setRange (0., 127.);
    speedSlider.setNumDecimalPlacesToDisplay(0);
    speedSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    speedSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(speedSlider);
    speedSlider.onValueChange = [this] {
        //todo
    };

    addAndMakeVisible (depthSliderLabel);
    depthSliderLabel.attachToComponent(&depthSlider, false);
    depthSliderLabel.setJustificationType(juce::Justification::centred);
    depthSlider.setRange (0., 127.);
    depthSlider.setNumDecimalPlacesToDisplay(0);
    depthSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    depthSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(depthSlider);
    depthSlider.onValueChange = [this] {
        // //todo
    };

    addAndMakeVisible (accSliderLabel);
    accSliderLabel.attachToComponent(&accSlider, false);
    accSliderLabel.setJustificationType(juce::Justification::centred);
    accSlider.setRange (0., 127.);
    accSlider.setNumDecimalPlacesToDisplay(0);
    accSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    accSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(accSlider);
    accSlider.onValueChange = [this] {
        //todo
    };
 
    addAndMakeVisible (vibratoSliderLabel);
    vibratoSliderLabel.attachToComponent(&vibratoSlider, false);
    vibratoSliderLabel.setJustificationType(juce::Justification::centred);
    vibratoSlider.setRange (0., 127.);
    vibratoSlider.setNumDecimalPlacesToDisplay(0);
    vibratoSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    vibratoSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(vibratoSlider);
    vibratoSlider.onValueChange = [this] {
        / //todo
    };

    addAndMakeVisible (vibrato25SliderLabel);
    vibrato25SliderLabel.attachToComponent(&vibrato25Slider, false);
    vibrato25SliderLabel.setJustificationType(juce::Justification::centred);
    vibrato25Slider.setValue (0.);
    vibrato25Slider.setRange (-1.0, 1.0);
    vibrato25Slider.setNumDecimalPlacesToDisplay(2);
    vibrato25Slider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    vibrato25Slider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour()); // transparent border
    addAndMakeVisible (vibrato25Slider);
    vibrato25Slider.onValueChange = [this] {
         //todo
    };
}

void MotorPanel::resized()
{
    auto area = getLocalBounds().reduced (5);
    motorLabel.setBounds (int(area.getWidth()/2)-40, 0, 80, 30);
    vibratoSlider.setBounds (10, 20, 60, 60);
    vibrato25Slider.setBounds (10, 115, 60, 60);
    speedSlider.setBounds (80, int(area.getHeight()/2)-20, 100, 100);
    depthSlider.setBounds (190, int(area.getHeight()/2)-20, 100, 100);
    accSlider.setBounds (300, int(area.getHeight()/2)-20, 100, 100);
}

void MotorPanel::paint (Graphics& g)
{
    auto area = getLocalBounds();
    g.setColour(Colour (0x32ffffff).darker(0.9999f));
    g.fillRoundedRectangle(0, 0, area.getWidth(), area.getHeight(), 10);
    g.setColour(Colour (0x32ffffff));
    g.drawVerticalLine(80, 50, area.getHeight()-20);
}

void MotorPanel::setSIO(SIO * s){ sIO = s;};

FlapPanel::FlapPanel()
{
    addAndMakeVisible (tremoloLabel);
    tremoloLabel.setText ("Tremolo", dontSendNotification);
    tremoloLabel.setJustificationType(juce::Justification::centred);
    tremoloLabel.setFont(22);

    addAndMakeVisible (flapAttackSliderLabel);
    flapAttackSliderLabel.attachToComponent(&flapAttackSlider, false);
    flapAttackSliderLabel.setJustificationType(juce::Justification::centred);
    flapAttackSlider.setRange (0., 127.);
    flapAttackSlider.setNumDecimalPlacesToDisplay(0);
    flapAttackSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    flapAttackSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(flapAttackSlider);
    flapAttackSlider.onValueChange = [this] {
     //todo
    };

    addAndMakeVisible (flapReleaseSliderLabel);
    flapReleaseSliderLabel.attachToComponent(&flapReleaseSlider, false);
    flapReleaseSliderLabel.setJustificationType(juce::Justification::centred);
    flapReleaseSlider.setValue (0.);
    flapReleaseSlider.setRange (-1.0, 1.0);
    flapReleaseSlider.setNumDecimalPlacesToDisplay(2);
    flapReleaseSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    flapReleaseSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour()); // transparent border
    addAndMakeVisible (flapReleaseSlider);
    flapReleaseSlider.onValueChange = [this] {
     //todo
    };

    addAndMakeVisible (flapSpeedSliderLabel);
    flapSpeedSliderLabel.attachToComponent(&flapSpeedSlider, false);
    flapSpeedSliderLabel.setJustificationType(juce::Justification::centred);
    flapSpeedSlider.setRange (0., 127.);
    flapSpeedSlider.setNumDecimalPlacesToDisplay(0);
    flapSpeedSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    flapSpeedSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(flapSpeedSlider);
    flapSpeedSlider.onValueChange = [this] {
    //todo
    };

    addAndMakeVisible (flapDepthSliderLabel);
    flapDepthSliderLabel.attachToComponent(&flapDepthSlider, false);
    flapDepthSliderLabel.setJustificationType(juce::Justification::centred);
    flapDepthSlider.setRange (0., 127.);
    flapDepthSlider.setNumDecimalPlacesToDisplay(0);
    flapDepthSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    flapDepthSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    addAndMakeVisible(flapDepthSlider);
    flapDepthSlider.onValueChange = [this] {
    //todo
    };
}

void FlapPanel::resized()
{
    auto area = getLocalBounds().reduced (5);
    tremoloLabel.setBounds (int(area.getWidth()/2)-40, 0, 80, 30);
    flapAttackSlider.setBounds (10, 20, 60, 60);
    flapReleaseSlider.setBounds (10, 115, 60, 60);
    flapSpeedSlider.setBounds (80, int(area.getHeight()/2)-20, 100, 100);
    flapDepthSlider.setBounds (190, int(area.getHeight()/2)-20, 100, 100);
}

void FlapPanel::paint (Graphics& g)
{
    auto area = getLocalBounds();
    g.setColour(Colour (0x32ffffff).darker(0.9999f));
    g.fillRoundedRectangle(0, 0, area.getWidth(), area.getHeight(), 10);
    g.setColour(Colour (0x32ffffff));
    g.drawVerticalLine(80, 50, area.getHeight()-20);
}

void FlapPanel::setSIO(SIO * s){ sIO = s;};

//------------------------------------------------
ButtonsPanel::ButtonsPanel()
{
    addAndMakeVisible (resetButton);
    resetButton.setButtonText ("Reset");
    resetButton.onClick = [this] {
//todo
    };

    addAndMakeVisible (pitchbendSliderLabel);
    pitchbendSliderLabel.attachToComponent(&pitchbendSlider, false);
    pitchbendSliderLabel.setJustificationType(juce::Justification::centred);
    pitchbendSlider.setValue (0.);
    pitchbendSlider.setRange (-1.0, 1.0);
    pitchbendSlider.setNumDecimalPlacesToDisplay(2);
    pitchbendSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    pitchbendSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour()); // transparent border
    addAndMakeVisible (pitchbendSlider);
    pitchbendSlider.onValueChange = [this] {
    sirenIO->setPitchBend(pitchbendSlider.getValue());
    };

    addAndMakeVisible (volumeSlider);
    volumeSliderLabel.attachToComponent(&volumeSlider, false);
    volumeSliderLabel.setJustificationType(juce::Justification::centred);
    volumeSlider.setRange (0., 127.);
    volumeSlider.setNumDecimalPlacesToDisplay(0);
    volumeSlider.setTextBoxStyle (Slider::TextBoxBelow, false, 80, 20);
    volumeSlider.setColour(Slider::ColourIds::textBoxOutlineColourId, juce::Colour());
    volumeSlider.onValueChange = [this] {
     //todo
    };
    addAndMakeVisible (flapPanel);
    addAndMakeVisible (motorPanel);
}

void ButtonsPanel::resized()
{
    resetButton.setBounds(5, 20, 100, 40);
    volumeSlider.setBounds (20, 115, 80, 80);
    pitchbendSlider.setBounds (150, 25, 80, 80);
    flapPanel.setBounds(250, 10, 300, area.getHeight()-20);
    motorPanel.setBounds(600, 10, 350, area.getHeight()-20);
}

void ButtonsPanel::paint (Graphics& g)
{
    g.fillAll (Colour (0x32ffffff));
}

void ButtonsPanel::setSIO(SIO * s){ sIO = s;};
```

---

<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:** [July 12, 2021, 8:16pm UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910/2 "2021-07-12T20:16:36Z")

</div>

It would be helpful to know in which line the access error happens. If it is outside your code usually it’s best to go back in the call stack until you reach your own code and start looking there.

The line

```auto
SIO* sIO;

```

is scary, you should initialise it with nullptr, but I understand that you are not accessing that pointer yet.  
It would be preferred to have a reference instead that is set from the constructor already.

---

<div class="post-metadata">

**Author:** ![sandoval31](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sandoval31](https://forum.juce.com/u/sandoval31)\
**Post date:** [July 16, 2021, 7:31am UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910/3 "2021-07-16T07:31:11Z")

</div>

Thank you very much @daniel for your answer. I tried to initialize my pointers with nullptr, but I’ve still got the error.

I tried to “go back in the call stack”, but I’m not sure to understand how to do (I work with Xcode). I found the lines in my code that seem to trigger the error:

```
std::ifstream myfile;
myfile.open(path, std::ios::binary);   

```

These lines seemed to work perfectly before, the path is ok, and I can’t figure out why adding a component in another class would have change something with these lines.

Furthermore, the error seems to happen in the thread: com.apple.audio.IOThread.client, that call a function in juce\_Memory.h.

I suppose the error has nothing to do the file reading, but rather to the creation of new component (but I can’t catch this with a breakpoint in my code).

---

<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:** [July 16, 2021, 7:46am UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910/4 "2021-07-16T07:46:22Z")

</div>

It depends what you do with the myfile. If you hand it to some RAII method (passing on ownership), then it would be deleted twice. That’s my hunch, but that is not visible in the code you posted…  
We need to see what you do with myfile.

Good luck

---

<div class="post-metadata">

**Author:** ![sandoval31](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sandoval31](https://forum.juce.com/u/sandoval31)\
**Post date:** [July 16, 2021, 8:32am UTC](https://forum.juce.com/t/exc-bad-access-on-child-components-creation/46910/5 "2021-07-16T08:32:57Z")

</div>

Ok. Basically, I just read a binary file (which is an array of numbers).  
The error is triggered by the first line of the following code:

```
myfile.open(path, std::ios::binary); // -> error in juce_Memory.h : readUnaligned(const void* srcPtr)

if (myfile.is_open())
{
    myfile.read(reinterpret_cast<char *>(tabAmp), sizeof tab); 
    myfile.close();
}
else std::cout << "Error. Binary file not found: " << dataFilePath + tabAmpFile << "\n";

```

The rest of the code (test to see if the file is well open) is not executed.

Do you think it can be related to the error? I would say that the error comes from another thread, and that the execution stops here only because it takes time to open the file. Is it possible?
