I have the following code:
void class::Testfunc()
{
QTimer* timer = new QTimer;
QObject::connect(timer, &QTimer::timeout, [this](){
emit Log("Time out...");
TestFunc(serverAddress, requestsFolderPath);
// deleteLater(); //*** why does this crash if used to replace the connect below?
});
connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
timer->setSingleShot(true);
timer->start(1000);
}
A single shot timer is created with a line connected to the lambda function, which registers the entrance to the lambda function every second (prints text to standard output) and calls the function again.
This works without a problem. However, if I delete the connect call for deleteLater (below the lambda function), but include the deleteLater call in the lambda function, the function will fail. It prints once and right after that, it fails while trying to delete a timer object.
What is the difference between the two calls to deleteLater in this instance and why placing deleteLater in a lambda function causes a problem here, while creating a separate connection works as expected, although both call deleteLater in response to a timeout signal timer?