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. connect with/without lambda - why not equivalent?
Forum Updated to NodeBB v4.3 + New Features

connect with/without lambda - why not equivalent?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 369 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.
  • J Offline
    J Offline
    Joachim W
    wrote on last edited by Joachim W
    #1
    class DoubleSpinBox : public QAbstractSpinBox {
        void f();
        void g() {
            ...
            connect(x, &X::sig, this, &DoubleSpinBox::f);
            // or
            connect(x, &X::sig, [this] { f(); });
        }
    }
    

    Aren't these two forms of the connect function equivalent? In a complex code, the first form works, while the second causes a segfault

    (gdb) p this
    $3 = (DoubleSpinBox * const) 0x555557178fc0
    (gdb) p *this
    $4 = {<QAbstractSpinBox> = {<No data fields>}, static staticMetaObject = {d = {superdata = {
            direct = 0x7ffff76eea00 <QAbstractSpinBox::staticMetaObject>}, 
          stringdata = 0x7ffff7d053a0 <(anonymous namespace)::qt_meta_stringdata_DoubleSpinBox>, 
          data = 0x7ffff7d05340 <qt_meta_data_DoubleSpinBox>, 
          static_metacall = 0x7ffff7a48740 <DoubleSpinBox::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, 
          relatedMetaObjects = 0x0, 
          metaTypes = 0x7ffff7ed1860 <qt_incomplete_metaTypeArray<(anonymous namespace)::qt_meta_stringdata_DoubleSpinBox_t, QtPrivate::TypeAndForceComplete<DoubleSpinBox, std::integral_constant<bool, true> >, QtPrivate::TypeAndForceComplete<void, std::integral_constant<bool, false> >, QtPrivate::TypeAndForceComplete<double, std::integral_constant<bool, false> > >>, extradata = 0x0}}, m_property = 0x55555718d4f0, m_old_dir = 0, m_step = 0.10000000000000001}
    

    How can it ever happen that the parent class has <No data fields>?

    JonBJ 1 Reply Last reply
    0
    • J Offline
      J Offline
      Joachim W
      wrote on last edited by
      #5

      The problem in my code was a hidden disconnect statement.

      To close this thread properly, let me summarize what I understood from the docs: The two forms are perfectly equivalent as long as sender and receiver are alive. They differ in how they react to destruction of the receiver: A connection established through form 1 will automatically be deleted, which is not the case for form 2, hence the segfault when this->f is called though this no longer exists.

      1 Reply Last reply
      1
      • J Joachim W
        class DoubleSpinBox : public QAbstractSpinBox {
            void f();
            void g() {
                ...
                connect(x, &X::sig, this, &DoubleSpinBox::f);
                // or
                connect(x, &X::sig, [this] { f(); });
            }
        }
        

        Aren't these two forms of the connect function equivalent? In a complex code, the first form works, while the second causes a segfault

        (gdb) p this
        $3 = (DoubleSpinBox * const) 0x555557178fc0
        (gdb) p *this
        $4 = {<QAbstractSpinBox> = {<No data fields>}, static staticMetaObject = {d = {superdata = {
                direct = 0x7ffff76eea00 <QAbstractSpinBox::staticMetaObject>}, 
              stringdata = 0x7ffff7d053a0 <(anonymous namespace)::qt_meta_stringdata_DoubleSpinBox>, 
              data = 0x7ffff7d05340 <qt_meta_data_DoubleSpinBox>, 
              static_metacall = 0x7ffff7a48740 <DoubleSpinBox::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, 
              relatedMetaObjects = 0x0, 
              metaTypes = 0x7ffff7ed1860 <qt_incomplete_metaTypeArray<(anonymous namespace)::qt_meta_stringdata_DoubleSpinBox_t, QtPrivate::TypeAndForceComplete<DoubleSpinBox, std::integral_constant<bool, true> >, QtPrivate::TypeAndForceComplete<void, std::integral_constant<bool, false> >, QtPrivate::TypeAndForceComplete<double, std::integral_constant<bool, false> > >>, extradata = 0x0}}, m_property = 0x55555718d4f0, m_old_dir = 0, m_step = 0.10000000000000001}
        

        How can it ever happen that the parent class has <No data fields>?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Joachim-W said in connect with/without lambda - why not equivalent?:

        connect(x, &X::sig, [&] { f(); });

        Don't forget that [&] passes all local variables by reference. If this is not your real code and your lambda accesses one of them, they would eb out-of-scope by the time the signal/slot connection activates. Could this be relevant to your situation?

        J 1 Reply Last reply
        0
        • JonBJ JonB

          @Joachim-W said in connect with/without lambda - why not equivalent?:

          connect(x, &X::sig, [&] { f(); });

          Don't forget that [&] passes all local variables by reference. If this is not your real code and your lambda accesses one of them, they would eb out-of-scope by the time the signal/slot connection activates. Could this be relevant to your situation?

          J Offline
          J Offline
          Joachim W
          wrote on last edited by
          #3

          @JonB I edited the question to replace [&] by [this] - same error though no other variables are captured.

          Christian EhrlicherC 1 Reply Last reply
          0
          • J Joachim W

            @JonB I edited the question to replace [&] by [this] - same error though no other variables are captured.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            The second will crash as soon as DoubleSpinBox is destroyed but x is still alive and emitting the signal as you don't pass a context to the connect function. The 3-arg connect() should therefore be avoided.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            4
            • J Offline
              J Offline
              Joachim W
              wrote on last edited by
              #5

              The problem in my code was a hidden disconnect statement.

              To close this thread properly, let me summarize what I understood from the docs: The two forms are perfectly equivalent as long as sender and receiver are alive. They differ in how they react to destruction of the receiver: A connection established through form 1 will automatically be deleted, which is not the case for form 2, hence the segfault when this->f is called though this no longer exists.

              1 Reply Last reply
              1
              • J Joachim W has marked this topic as solved on

              • Login

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