[Development] leak in QMetaObject?

Olivier Goffart olivier at woboq.com
Thu Jul 14 17:10:40 CEST 2016

On Donnerstag, 14. Juli 2016 16:33:26 CEST Thomas Senyk wrote:
> Hi,
> I lately wanted to validate that a connecting&disconnecting to a lambda
> will not leak the lambda object .. and found that more then that is leaked.
> Here is my example: http://paste.ofcode.org/36dmWCT8ddF6Zaqb9csKZNv
> After the 10million connections are executed and disconnected (all of
> them a successful), I assume a drop in memory consumption and more
> importantly a drop of cpu load to close to 0%.
> I see neither one nor the other. I see continues 100% consume with no
> memory consumption drop what so ever.
> I did a quick peak into QMetaObject::activate and it seems no clean up
> happened .. the list returned in
>   list = &connectionLists->at(signal_index);
>   (line #3660 in 5.6 checkout from today)
> seems rather long! (10million?)
> However all "if (!c->receiver)" shows no receiver and therefor will
> continue right away .. at least
> Is this expected? A bug? ... I'm happy to fill a bug report, I just
> wanted to get some feedback, maybe I'm just doing something wrong

The lambda object and its capture is destroyed by the call to 

The internal data structre (QObjectPrivate::Connection) is deleted by 
QObjectPrivate::cleanConnectionLists which is actually only called when 
a new connection is made to this object.

I don't think there is a leak, but it is true that some memory is only free'ed 
when you add new connection or that the object is destroyed. (but the lambda 
object is destroyed right after the disconnection)

> Some background:
> I wanted to check if a single-shot-connection (connection to a signal
> and disconnect at first execute, using a lambda as slot) would be a
> feasible thing to do.

You can do that already:

 QTimer::sigleShot(100, [] { qDebug() << "hello"; });


Woboq - Qt services and support - https://woboq.com - https://code.woboq.org

More information about the Development mailing list