Qt5 new signal to lambda connections can result memory leak?
-
I found Qt5 uses a new kind of signal connection. It's very convenient to connect signals to Lambda expressions. But someone said that this connection may result memory leak. Is that really so?
Some people put forward a couple of patches to solve this behavior. I'm using Qt5.9.1 on windows. I wonder if this problem still exists. In Qt5, whether this method is recommended for use?QObject::connect(directoryComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int a){listIndex = a;});
Thanks for any advice!
The information is as followed:
/*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?*/ //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?*/
Someone put forward the solution, the patch is here!
-
Hi
Since lambdas is just a nameless function/functor/callable, im
not sure how it could leak ?
Can you provide more info /where you have seen it ? -
@Qt_crazyer
if you allocated memory inside your lambda and do not free that, than yes, thats a leake.g
QTimer timer; timer.setInterval(0); connect(&timer, &QTimer::timeout, []{QImage *img = new QImage(100,100,QImage::Format_RGB32);}); timer.start();
but that as nothing to do with it being a lambda function.
As far as I know theres no issue with lambdas and Qts Signal Slot.
Could you give us some more information, what you mean?
-
@Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:
Some people put forward a couple of patches to solve this behavior.
Can you post the link to these patches that you are referring to?
-
@Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:
But someone said that this connection may result memory leak
Could you please provide the source(s) for such statement?
Have you tested your application with some tool i.e. Valgrind so you could provide evidence? -
The original discussion about the leak is copied from https://stackoverflow.com/questions/13847507/qt5-new-signal-to-lambda-connections-memory-leak (2012)
@Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:
Someone put forward the solution, the patch is here!
The patch was merged into Qt in 2012. The leak was fixed 5 years ago, with Qt 5.0.1.
So, Qt 5.9 does not have this leak.