OwnedArray and a JUCE Message Thread (1): EXC_BAD_ACCESS


I’m trying to dynamicaly create sliders (I know the number in advance):

OwnedArray cSliders;

In MainComponent:

for(int i = 0; i < 20; i++)
    cSliders.add(new Slider("slider" + String(i)));
    cout << cSliders.operator[](i) << "\n";

Then add some attributes:

cSliders.operator (0)->setRange(0.0, 1000.0, 0.0001);
cSliders.operator (1)-> setRange(0.0, 0.5, 0.0001);

etc. ////

But a problem comes in MainComponent::resized()

cSliders.operator (0)->setBounds(something relative));
cSliders.operator (1)->setBounds(something relative));

etc. ////

Causes a JUCE Message Thread (1): EXC_BAD_ACCESS (code=1, address=0x28) with a priori a problem with getWidht() in resized according to the debugger.

I do not understand (I am beginner in programming and Juce that I learn…)

And a last thing, in case of OwnedArray, is the pointer changes each time I call the operator ?

Maybe could you help me please?

My guess is that you are calling setSize before the cSliders have been initialised, which means a resized is triggered and then you’re trying to access things that don’t yet exist.

Either make sure to not call setSize before, or wrap the setBounds in your resized method with a test that you actually do have some cSliders present:

    // call setbounds() on all your sliders

FYI, this cSliders.operator[](1)->setBounds(something relative)); is a bit clunky and unneeded, you can just do cSliders[1]->setBounds(...) instead.
Also for future reference, when posting code on the forum, it’s much more readable when surrounded by either a single back tick `, for when you want some code in-line, or three of them when pasting multiline code.

I thank you very much Richie,

When setsize is called after the cSliders initialisation the problem is solved.

I memories your recommendations for future reference, and your syntactic advice for operator as well.

See you