[Interest] Who disconnects lambda expression slots?

Nikos Chantziaras realnc at gmail.com
Sat Dec 15 05:50:07 CET 2012


On 15/12/12 06:33, Thiago Macieira wrote:
> On sábado, 15 de dezembro de 2012 05.46.39, Nikos Chantziaras wrote:
>> On 15/12/12 05:19, Thiago Macieira wrote:
>>>  [...]
>>> It automatically disconnects when the destruction reaches the QObject
>>> destructor.
>>
>> Which QObject destructor?  The receiver is being destroyed before the
>> sender:
>
> The receiver's. If the signal is emitted *during* the receiver's destruction,
> you have to analyse this case.
>
>>     class Foo: public QObject {
>>         Q_OBJECT
>>     public:
>>         Foo(QObject* obj)
>>         {
>>             QObject::connect(obj, &QObject::objectNameChanged,
>>             [this](const QString& objectName) {
>>                 ++this->i;
>>             });
>>         {
>>
>>         int i = 0;
>>
>>         //...
>>     };
>>
>>     // ...
>>     Foo* foo = new Foo(someQObjectPtr);
>>     delete foo;
>>
>> So how does the destructor of Foo know how to disconnect when foo is
>> deleted?
>
> 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.  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();
         });
     }




More information about the Interest mailing list