MacBook Pro Speakers (8): EXC_BAD_ACCESS (code=1, address=0x0) ERROR

Hello everybody I am working on a simple synth and I’ve got a weird error when trying to pass the attack parameter through a pointer, the compiler runs the program but when I load it into the PluginHost crashes and gives me an access error: MacBook Pro Speakers (8): EXC_BAD_ACCESS (code=1, address=0x0), so far I’ve understood the problem is that the pointer I am using to address the attack parameter is NULL, but really I can not understand why is that, I will give you a bit of code to look at as well. thanks in advance

  void getParam (float *attack)

here is where i pass the value to the method, that is from inside of the ‘processorblock’ method

        if ((myVoice = dynamic_cast<SynthVoice*>(mySynth.getVoice(i))))
            float* newFloatPtr = (float*) tree.getRawParameterValue("Attack");

of course the getParam functions is a member function of the SynthVoice class whom object is myVoice

First thing to do here is obviously to put a breakpoint in the line
float* newFloatPtr = (float*) tree.getRawParameterValue("Attack");
and see which address newFloatPtr has. I guess it is NULL, which would explain the error. Probably the argument “Attack” passed to it does not match the exact name that your attack parameter has been created with? How do you create the attack parameter in the first place?

Now even if this would return a non-null pointer this line is generally suspicious for two reasons:

  1. The return type of getRawParameterValue is std::atomic<float>* and you cast it into a float* with a hard C-Style cast :astonished: These are two different, incompatible types, so casting a pointer from the one to the other is likely to cause all kind of trouble. Don’t do that. I furthermore don’t see why you need a pointer here at all. You could simply dereference it in place, like float attack = *tree.getRawParameterValue("Attack");
  2. The intention of this method to return a pointer to the parameters underlying atomic value is that you can call this function once and then store this pointer / a reference as a member. This is by far more efficient than looking up the parameter again and again when you need it
1 Like

Thank you for replying, I have applied your solution and that still gives me the same error, at this point I am pretty sure the problem is in the way I have created the parameter, so, that’s how I’ve done it.
First I’ve instantiate a pointer to object of the parameter class, and then I’ve initiated that object with this line of code (inside the processor constructor).

 addParameter (attackParam = new juce::AudioParameterFloat ("attack", "Attack", 0.1f, 5000.0f, 0.1f)); 

Also, I have changed the ID of the getRawParameterValue into "attack’ (was ‘Attack’ before and I’m sure that was wrong).
I am fairly new to C++ and JUCE in general, do you think I ve wrongly created the parameter??

Fixed it thanks!! Turns out that was the wrong way of creating and adding a parameter to the TreeState, thanks for your time!