How to fix this exception?

Maincomponent.cpp

#include "MainComponent.h"

//==============================================================================
MainComponent::MainComponent()
{
    setSize (600, 400);
    addAndMakeVisible(checkTimeButton);
    addAndMakeVisible(timeLabel);
    checkTimeButton.onClick();
    currentTime = juce::Time::getCurrentTime();
    lastTime = currentTime;
}

MainComponent::~MainComponent()
{
}

void MainComponent::checkTimeFunction() {
    currentTime = juce::Time::getCurrentTime();
    juce::RelativeTime timeDifference = (currentTime - lastTime);
    timeLabel.setText(juce::String(timeDifference.inMilliseconds()) + " milliseconds", juce::dontSendNotification);
    lastTime = currentTime;
}
//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
    // (Our component is opaque, so we must completely fill the background with a solid colour)
    g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));

    
}

void MainComponent::resized()
{
    // This is called when the MainComponent is resized.
    // If you add any child components, this is where you should
    // update their positions.
}

Maincomponent.h

#pragma once

#include <JuceHeader.h>

//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/
class MainComponent  : public juce::Component
{
public:
    //==============================================================================
    MainComponent();
    ~MainComponent() override;

    //==============================================================================
    void paint (juce::Graphics&) override;
    void resized() override;
    void checkTimeFunction();

private:
    //==============================================================================
    // Your private member variables go here...
    juce::TextButton checkTimeButton;
    juce::Label timeLabel;
    juce::Time currentTime = juce::Time::getCurrentTime();
    juce::Time lastTime = currentTime;
    


    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

In the lower right part of the UI of Visual Studio, there is a “Call Stack” tab. You can use it to figure out what in your code is causing that exception.

checkTimeButton.onClick();

It expects a lamba function

This is the error. What are you excepting this line to do? I guess you got something wrong about the meaning of that member. onClick is a std::function object that is unassigned by default. You can assign any custom function to it that then will be called every time the button is clicked.

Examples 1: A free function

void myFunction()
{
    DBG ("myFunction called");
}

MainComponent::MainComponent()
{
    //... whatever

    checkTimeButton.onClick = myFunction;
}

Here, each click on checkTimeButton would lead to myFunction being called. Note that if this would be a member function, you have to declare it static.

Example 2: A lambda

MainComponent::MainComponent()
{
    //... whatever

    checkTimeButton.onClick = []()
    {
        DBG ("lambda called");
    };
}

This would call the assigned lambda function each time the button is clicked. Most people will use it this way, as it allows you to capture the this pointer and call non-static member function from within the lambda.

What you wrote above is basically the internal code that is called by the button when you click if, combined with a safety check if the std::function has been assigned at all.

You simply call this function that has not been assigned, which obviously leads to the crash. You basically do this here

std::function<void()> fn = nullptr;
fn(); // crash – how would you call a nullptr

Hope this will help you to understand that crash. But what was your original intention writing this line? What did you expect it to do?

1 Like

How to fix C2120?

Note, no bracket’s after onClick in PluginPenguin’s example

With brackets it’s trying to assign a lambda to the result of a function call, rather than assigning the lambda to a variable called onClick.

it didn’t fix

So your code is now

checkTimeButton.onClick = [this]() { checkTimeFunction(); };

And are still getting the same error? This would be quite surprising to me, so I guess you still have a syntax error and I couldn’t make the point as clear as I wanted in my post above. Just a side note, we are really willing to help programmers on all levels of experience here, but it’s not that satisfying for us to only get such one-liner responses here. Tell us a bit more what you tried, why you think xy should work etc. That makes it a lot easier to actually help you :slight_smile: Furthermore, I guess nearly no one knows the Microsoft specific compiler error codes you post here (e.g. C2120) from memory, so better ask questions like e.g. “Why am I getting a compiler error in this lambda assignment”.

And then, if you want to educate yourself a bit about lambda syntax to maybe help yourself with your issues, I think cppreference gives you a great overview of them here https://en.cppreference.com/w/cpp/language/lambda

Although this website might seem quite difficult to understand especially for newcomers and the examples often are written in a way that they try to use a lot of pro level C++ features in a few lines of example code, I’d advise you to use it as your first resource when struggling with C++ syntax issues

4 Likes