samedi 20 août 2016

EXC_BAD_ACCESS with lambda capture

First of all, excuse me if this seems obvious - I'm sort of new to C++. I have been looking in to this, but I haven't found anything that was particulary helpful.

When trying to access a captured variable within a lambda, my application crashes and I have no idea why. I don't think that the object has been deleted, as when putting a breakpoint where it crashes and using CLion's debugger, CLion shows that the object exists.

A code sample will probably help me explain this:

//Create the progress dialog
QProgressDialog *progDialog = new QProgressDialog(tr("Opening Project…\nExtracted: 0 (0.0%)\nWaiting…"), nullptr, 0, 0, this);

// ... Some code here

//Declare a function to be passed as a callback
std::function<void (int minValue, int maxValue)> *progRangeChangedCallback = nullptr;

// ... More code here

//Create the lambda
//I capture progDialog (The progress dialog)
auto progRangeChangedCallbackLambda = [&progDialog](int newMin, int newMax) {
    //Putting a breakpoint here reveals that progDialog exists
    //CLion even autocompletes the below functions
    //when trying to evaluate an expresion
    progDialog->setMinimum(newMin); //EXC_BAD_ACCESS: Crashes happen here!
    progDialog->setMaximum(newMax);
};

// ... Even more code here

//Put the lambda in a std::function
progRangeChangedCallback = new std::function<void (int minValue, int maxValue)>(progRangeChangedCallbackLambda);

// ... More code

//Pass the std::function object as a callback to a new thread
//This extends QThread
OpenProjectThread *thread = new OpenProjectThread(filePath, this, progChangedCallback, progRangeChangedCallback, onSuccessCallback, onErrorCallback);
thread->start();

Aucun commentaire:

Enregistrer un commentaire