[Interest] Who disconnects lambda expression slots?

Nikos Chantziaras realnc at gmail.com
Sat Dec 15 04:46:39 CET 2012


On 15/12/12 05:19, Thiago Macieira wrote:
> On sábado, 15 de dezembro de 2012 03.45.56, Nikos Chantziaras wrote:
>> So I take it that means we should always manually disconnect in such
>> cases.  Thinking about it, it seems possible to have done this
>> automatically, but only for QObjects and through a connect() function
>> that would accept:
>>
>>       connect(const QObject* sender, PointerToMemberFunction signal,
>>               const QObject* receiver, Functor functor);
>>
>> In that case, connect() would be able to know when 'receiver' gets
>> destroyed and automatically do a disconnect.
>
> It automatically disconnects when the destruction reaches the QObject
> destructor.

Which QObject destructor?  The receiver is being destroyed before the 
sender:

   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?




More information about the Interest mailing list