Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt5 new signal to lambda connections can result memory leak?
Forum Update on Monday, May 27th 2025

Qt5 new signal to lambda connections can result memory leak?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 6 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Qt_crazyerQ Offline
    Qt_crazyerQ Offline
    Qt_crazyer
    wrote on last edited by JKSH
    #1

    I found Qt5 uses a new kind of signal connection. It's very convenient to connect signals to Lambda expressions. But someone said that this connection may result memory leak. Is that really so?
    Some people put forward a couple of patches to solve this behavior. I'm using Qt5.9.1 on windows. I wonder if this problem still exists. In Qt5, whether this method is recommended for use?

    QObject::connect(directoryComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int a){listIndex = a;});
    

    Thanks for any advice!

    The information is as followed:

    /*The new Qt5 signals and slots syntax allows us to connect signals not only to slots, but also to plain old functions and functors/lambdas. Now the problem is, that lambdas are essentialy objects with () operator, and when you connect signals to them, they get copied somewhere in qt internal classes. And, when you disconnect the signal from that functor, it stays in qt internals. I fail to understand, is that a normal behaviour? Or maybe there is a way to destroy those functional objects after disconnection?*/
    
    //example
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTimer* timer = new QTimer();
    
        QSharedPointer<QMetaObject::Connection> connection(new QMetaObject::Connection());
    
        //functor is created and gets copied inside qt internals, connection variable is captured
        //inside the functor
    
        *connection.data() = QObject::connect(timer, &QTimer::timeout, [=]
        {
            qDebug() << "disconnected";
            QObject::disconnect(*connection.data());
        });
    
        timer->start(10000);
    
        return a.exec();
    }
    
    //example
    
    /*Now when i look at strong reference count of connection variable after the slot disconnection, it stays 2, which means that the functor object itself is still alive and well, though it is of no use to me now. Do I miss something?*/
    
    

    Someone put forward the solution, the patch is here!

    J.HilkJ JKSHJ 2 Replies Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Since lambdas is just a nameless function/functor/callable, im
      not sure how it could leak ?
      Can you provide more info /where you have seen it ?

      Qt_crazyerQ 1 Reply Last reply
      1
      • Qt_crazyerQ Qt_crazyer

        I found Qt5 uses a new kind of signal connection. It's very convenient to connect signals to Lambda expressions. But someone said that this connection may result memory leak. Is that really so?
        Some people put forward a couple of patches to solve this behavior. I'm using Qt5.9.1 on windows. I wonder if this problem still exists. In Qt5, whether this method is recommended for use?

        QObject::connect(directoryComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int a){listIndex = a;});
        

        Thanks for any advice!

        The information is as followed:

        /*The new Qt5 signals and slots syntax allows us to connect signals not only to slots, but also to plain old functions and functors/lambdas. Now the problem is, that lambdas are essentialy objects with () operator, and when you connect signals to them, they get copied somewhere in qt internal classes. And, when you disconnect the signal from that functor, it stays in qt internals. I fail to understand, is that a normal behaviour? Or maybe there is a way to destroy those functional objects after disconnection?*/
        
        //example
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QTimer* timer = new QTimer();
        
            QSharedPointer<QMetaObject::Connection> connection(new QMetaObject::Connection());
        
            //functor is created and gets copied inside qt internals, connection variable is captured
            //inside the functor
        
            *connection.data() = QObject::connect(timer, &QTimer::timeout, [=]
            {
                qDebug() << "disconnected";
                QObject::disconnect(*connection.data());
            });
        
            timer->start(10000);
        
            return a.exec();
        }
        
        //example
        
        /*Now when i look at strong reference count of connection variable after the slot disconnection, it stays 2, which means that the functor object itself is still alive and well, though it is of no use to me now. Do I miss something?*/
        
        

        Someone put forward the solution, the patch is here!

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Qt_crazyer
        if you allocated memory inside your lambda and do not free that, than yes, thats a leak

        e.g

        QTimer timer;
        timer.setInterval(0);
        
        connect(&timer, &QTimer::timeout, []{QImage *img = new QImage(100,100,QImage::Format_RGB32);});
        timer.start();
        

        but that as nothing to do with it being a lambda function.

        As far as I know theres no issue with lambdas and Qts Signal Slot.

        Could you give us some more information, what you mean?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • M Offline
          M Offline
          mchinand
          wrote on last edited by
          #4

          @Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:

          Some people put forward a couple of patches to solve this behavior.

          Can you post the link to these patches that you are referring to?

          1 Reply Last reply
          1
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:

            But someone said that this connection may result memory leak

            Could you please provide the source(s) for such statement?
            Have you tested your application with some tool i.e. Valgrind so you could provide evidence?

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • mrjjM mrjj

              Hi
              Since lambdas is just a nameless function/functor/callable, im
              not sure how it could leak ?
              Can you provide more info /where you have seen it ?

              Qt_crazyerQ Offline
              Qt_crazyerQ Offline
              Qt_crazyer
              wrote on last edited by
              #6

              @mrjj I've uploaded the information I've seen! Thank you.

              1 Reply Last reply
              1
              • Qt_crazyerQ Qt_crazyer

                I found Qt5 uses a new kind of signal connection. It's very convenient to connect signals to Lambda expressions. But someone said that this connection may result memory leak. Is that really so?
                Some people put forward a couple of patches to solve this behavior. I'm using Qt5.9.1 on windows. I wonder if this problem still exists. In Qt5, whether this method is recommended for use?

                QObject::connect(directoryComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int a){listIndex = a;});
                

                Thanks for any advice!

                The information is as followed:

                /*The new Qt5 signals and slots syntax allows us to connect signals not only to slots, but also to plain old functions and functors/lambdas. Now the problem is, that lambdas are essentialy objects with () operator, and when you connect signals to them, they get copied somewhere in qt internal classes. And, when you disconnect the signal from that functor, it stays in qt internals. I fail to understand, is that a normal behaviour? Or maybe there is a way to destroy those functional objects after disconnection?*/
                
                //example
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QTimer* timer = new QTimer();
                
                    QSharedPointer<QMetaObject::Connection> connection(new QMetaObject::Connection());
                
                    //functor is created and gets copied inside qt internals, connection variable is captured
                    //inside the functor
                
                    *connection.data() = QObject::connect(timer, &QTimer::timeout, [=]
                    {
                        qDebug() << "disconnected";
                        QObject::disconnect(*connection.data());
                    });
                
                    timer->start(10000);
                
                    return a.exec();
                }
                
                //example
                
                /*Now when i look at strong reference count of connection variable after the slot disconnection, it stays 2, which means that the functor object itself is still alive and well, though it is of no use to me now. Do I miss something?*/
                
                

                Someone put forward the solution, the patch is here!

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

                The original discussion about the leak is copied from https://stackoverflow.com/questions/13847507/qt5-new-signal-to-lambda-connections-memory-leak (2012)

                @Qt_crazyer said in Qt5 new signal to lambda connections can result memory leak?:

                Someone put forward the solution, the patch is here!

                The patch was merged into Qt in 2012. The leak was fixed 5 years ago, with Qt 5.0.1.

                So, Qt 5.9 does not have this leak.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                7

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved