The new Qt5 signals and slots syntax allows us to connect signals not only to slots, but also to plain old functions and functors/lambdas. Now the problem is, that lambdas are essentialy objects with () operator, and when you connect signals to them, they get copied somewhere in qt internal classes. And, when you disconnect the signal from that functor, it stays in qt internals. I fail to understand, is that a normal behaviour? Or maybe there is a way to destroy those functional objects after disconnection?
Here's an example:
//example
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTimer* timer = new QTimer();
QSharedPointer<QMetaObject::Connection> connection(new QMetaObject::Connection());
//functor is created and gets copied inside qt internals, connection variable is captured
//inside the functor
*connection.data() = QObject::connect(timer, &QTimer::timeout, [=]
{
qDebug() << "disconnected";
QObject::disconnect(*connection.data());
});
timer->start(10000);
return a.exec();
}
//example
Now when i look at strong reference count of connection variable after the slot disconnection, it stays 2, which means that the functor object itself is still alive and well, though it is of no use to me now. Do I miss something?
QMetaObject::Connection
? That would explain why it's keeping a reference to the function. – Nunuance