Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Qt5 new signal to lambda connections can result memory leak?

    General and Desktop
    6
    7
    2833
    Loading More Posts
    • 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_crazyer
      Qt_crazyer last edited by JKSH

      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.Hilk JKSH 2 Replies Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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_crazyer 1 Reply Last reply Reply Quote 1
        • J.Hilk
          J.Hilk Moderators @Qt_crazyer last edited by

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          1 Reply Last reply Reply Quote 3
          • M
            mchinand last edited by

            @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 Reply Quote 1
            • Pablo J. Rogina
              Pablo J. Rogina last edited by

              @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 Reply Quote 1
              • Qt_crazyer
                Qt_crazyer @mrjj last edited by

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

                1 Reply Last reply Reply Quote 1
                • JKSH
                  JKSH Moderators @Qt_crazyer last edited by JKSH

                  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 Reply Quote 7
                  • First post
                    Last post