Getting JUCE Message Thread (1): EXC_BAD_ACCESS

Getting this error in JUCE when trying to abstract away the buttonClicked handler.

My code looks like this:


    class Button : public juce::Button::Listener,
                  public BaseComponent,
                  public juce::TextButton
          BaseHandler *);

      void buttonClicked(juce::Button *) override;
    void Button::buttonClicked(juce::Button *btn)
      // This is where the error happens
      // or handler->handle(btn);

And this is how the BaseHandler and a concrete handler look like:

    class BaseHandler
      virtual void handle(juce::Component *) = 0;
    void PlayHandler::handle(juce::Component *)
      std::cout << "From operator" << std::endl;

The error happens when invoking the handle method in Button::buttonClicked(). This is how I instantiate a button:

    PlayHandler ph;
    buttons.push_back(new Button("Play", 80, 40, &ph));

Why would I be getting “JUCE Message Thread (1): EXC_BAD_ACCESS (code=1, address=0x358001a9f48094)” here? I did this exact same thing but with lambdas instead of the BaseHandler implementation, and it was working fine.

Looks like your PlayHander is created on the stack, which will then go out of scope and therefore you hold a dangling pointer to &ph and the code explodes when trying to dereference it.

1 Like

Also this looks like an overcomplicated design.

For each juce button instance, you can set the onClick member functional to do whatever you want without the need for subclassing