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. Bug in disconnect when using lambdas
Forum Updated to NodeBB v4.3 + New Features

Bug in disconnect when using lambdas

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 2.4k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    philk
    wrote on last edited by
    #1

    Using this code:

    @
    obj->connect(sender, &Sender::signal, [] {
    });

    // later
    obj->deleteLater();
    @

    The object will be deleted, but the connection will not be broken, and the sender can still signal, the lambda will be called, but of course it will crash. Is this a known limitation of using lambdas?

    J.HilkJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Good question, I think you should ask it on the interest mailing list, you'll find Qt's developers/maintainers there (this forum is more user orienteD)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lqsa
        wrote on last edited by
        #3

        After read this: [https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f#.52geilaqe](How C++ lambda expressions can improve your Qt code), you need to pass a context argument to connect:

        obj->connect(sender, &Sender::signal, obj, [] {
        
        1 Reply Last reply
        1
        • P philk

          Using this code:

          @
          obj->connect(sender, &Sender::signal, [] {
          });

          // later
          obj->deleteLater();
          @

          The object will be deleted, but the connection will not be broken, and the sender can still signal, the lambda will be called, but of course it will crash. Is this a known limitation of using lambdas?

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

          @philk

          it falls in the same category as Lambdas do not work with Qt::UniqueConnection found here

          Also:

          @Wiki said in New Signal Slot Syntax:

          //Section Disconnecting in Qt5

          Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card) In particular, does not work with static function, functors or lambda functions.

          -> not automatic disconnects when deleting the sender.


          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
          1

          • Login

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