[Interest] Who disconnects lambda expression slots?

Thiago Macieira thiago.macieira at intel.com
Sat Dec 15 06:07:24 CET 2012


On sábado, 15 de dezembro de 2012 06.50.07, Nikos Chantziaras wrote:
> On 15/12/12 06:33, Thiago Macieira wrote:
> > I don't see your point. QObject::connect adds the connection object to a
> > list. QObject::~QObject destroys the list.
> 
> What I don't get is how QObject::connect() can tell where it was called
> from.

It can't.

> How does it get to know that the 'foo' object is doing the
> connection?  What if the connection is made from a non-member function?
>   Like:
> 
>      static void doConnection(QObject* sender, QObject* receiver)
>      {
>          QObject::connect(sender, &QObject::someSignal, [receiver]() {
>              receiver->doSomething();
>          });
>      }

That's the point. It can't. This connection has absolutely no way of knowing 
when the receiver is deleted. It's entirely up to you to deal with it.

There's an overload that accepts a receiver object and a functor as 
parameters. The receiver object is used to track the lifetime of the 
connection: if either the sender or receiver is destroyed, the connection is 
broken properly.

Still, the connection mechanism cannot tell if the destruction has started. It 
can only tell whether ~QObject has run its course. So even the 4-argument 
overload -- the one that automatically disconnects -- can run into problems by 
calling receiver->doSomething() after destruction has begun. That was my 
example.

The old-style connection, using a virtual function, had a built-in safety 
mechanism.

-- 
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/275e9256/attachment.sig>


More information about the Interest mailing list