PDA

View Full Version : Why is the lambda crashing my program?



ayanda83
17th January 2017, 19:00
Hi there guys, I am new to C++11 expressions. I got a lambda below and it crashes my program.
void Joburg_Page::connect_to_HTML_tags(bool ok)
{
QString docTitle;

this->runJavaScript("document.title"
"}", [&docTitle](const QVariant &data){docTitle = data.toString();});

qDebug() << docTitle <<endl;
}
}

Why is it necessary that I must declare docTitle is a member of the class before the above code works?

anda_skoa
18th January 2017, 10:13
runJavaScript is asynchronous, the scope of connect_to_HTML_tags() has ended before the lambda is being executed.
docTitle has ceased to exist by the time it is accessed.

Cheers,
_