EXC_BAD_ACCESS when setting bounds

In my Plugin the User is able to add some Effects, which are separate classes. Every time an effect is added, a new instance of this class is created and so are some sliders and labels. So far everything worked totally without any problems. But now I redesigned my code such that Pointers to the labels and Sliders are stored within an array and later on, I iterate over the array to set the bounds. and here the Problem occurs. when the call componentPointer->setBounds(x,y,width,hight) is made, the Programm crashes and causes a EXC_BAD_ACCESS Message. the Problem seems to be caused from the getWidth() method, but that is literally all I know. The funny part…sometimes.,…in really rare cases. it Works…meaning…the program does not crash…but the sliders are still not shown.
Here is what I already tried:
Using a MessageManagerLock
setting The bounds before Storing the Pointers
and yes, the pointers are checked against nullptr
Do you have any ideas what could cause the problem? or maybe just an idea what else I could try to figure out the prob? I crunched my gears for like 3 hours and am totally clueless
Thanks a lot guys

You need to show your actual code, it’s very hard to say anything based on what you’ve explained.

thanks for the quick response.

The Array is defined like this:

Slider* defaultSliderLayout[defaultLayoutPositions];

Adding the Pointers is just done with a method that takes a Pointer to the component and an integer in which position the pointer shall be stored. and then just:

defaultSliderLayout[column] = slider;

Allright, here is where I iterate over the arrays:
(all the unit- and subdivision things are stored in a static const class, and the class uses its namespace, but even changing to bare numbers does not change anything

for(int col = 0; col<defaultLayoutPositions; col++){
    int x =(col+defaultLayoutStartOffset)*unit;
    Label* lab = defaultLabelLayout[col];
    if(lab!= nullptr){
        lab->setBounds(x, (subdivider-1)*unitSubDivision, unit, unitSubDivision);
    Slider* slid = defaultSliderLayout[col];
        slid->setBounds(x, 0, unit, (subdivider-1)*unitSubDivision);

That code still doesn’t show how you are creating the sliders.

The Sliders are just created with

    Slider sliderName;

(making them public does not help…)
But there is one other thing I haven’t considered yet. The code I posted is written within a parent class, but the Sliders are created in classes that derive from that one. That is differs from my previous design, where all the components were set within the child classes. I would not think that this could be a problem, but it might be worth mentioning.

Are you sure you are calculating the indexes into the arrays correctly? The null pointer element check is not going to help detecting wrong array index accesses. Also the arrays won’t contain null pointers by default so if you don’t fill every array element explicitly with either a valid pointer or a null pointer, you are going to have undefined behavior.

1 Like

WOW thanks
now THIS was a really good tip. C++ and it’s undefined behavior.
I defined the Pointerarrays within the header. Then the first thing I did within the Constructor of the parent class was adding a nullptr to every position within the pointer array and that really did the trick. (I used to program in c# and java so I am used to “everything I add is automatically null”-behavior…)
thanks lot again

In C++ there is the principle “don’t pay for what you don’t need” and that is particularly true for primitive data types. Variables that are not explicitly initialized can be left in indeterminate states and so on by the compiler. (So raw pointers are not null by default, integers are not zeros by default and so on. They can end up zeros by chance, though. :wink: In debug builds the compilers may use magic initial values for pointers for example so you can more easily spot them in the debugger.)


Thanks, I definitely will keep that in mind :smiley: