[Interest] Who disconnects lambda expression slots?

Thiago Macieira thiago.macieira at intel.com
Sat Dec 15 02:12:00 CET 2012


On sábado, 15 de dezembro de 2012 02.53.58, Nikos Chantziaras wrote:
> Since it's now possible to use lambdas as slots:
> 
>      connect(sender, &SenderClass::connected, [this] () {
>          this->doSomething();
>      };
> 
> I have to wonder what is supposed to happen when the object where the
> lambda appears gets destroyed.  In this particular example, if the
> lambda executes, dereferencing 'this' is no longer valid.
> 
> The documentation:
> 
>    http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#connect-5

Note that connect() returns a QMetaObject::Connection object now and there's a 
disconnect() overload that takes such an object.

Also note how connecting to a lambda changes the delivery behaviour during 
destruction. That is:

	connect(sender, SIGNAL(connected()), SLOT(doSomething));
versus your example. During the destruction of this class, delivery to 
doSomething will be stopped due to the virtual table being destroyed, but the 
lambdas won't. You may end up calling this->doSomething() when the class is no 
longer valid.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121214/aaadd998/attachment.sig>


More information about the Interest mailing list