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 cause exception triggered (Beginner)
Forum Updated to NodeBB v4.3 + New Features

connect cause exception triggered (Beginner)

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 4 Posters 9.2k Views 1 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.
  • DumaPlusPlusD DumaPlusPlus

    of course i have this is the complete code:

    void MainWindow::build_tab_from_plugin(Plugin *plugin)
    {
      QWidget* tab = new QWidget();
    
      QVBoxLayout* layout = new QVBoxLayout();
      QPushButton* send = new QPushButton();
      QPlainTextEdit* textBox = new QPlainTextEdit();
      QLineEdit* resultBox = new QLineEdit();
    
      connect(send,&QPushButton::clicked,[&](){ resultBox->setText(plugin->send_paste(textBox->document()->toPlainText())); });
    
      resultBox->setReadOnly(true);
      send->setText("Send paste");
    
      layout->addWidget(send);
      layout->addWidget(textBox);
      layout->addWidget(resultBox);
      tab->setLayout(layout);
    
      ui->tabWidget->addTab(tab,plugin->name());
    
    }
    

    and i benefit from this thread to ask "the objects init in heap store are delete by main window is some way or i have to manually delete them in destructor?"

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #4

    @DumaPlusPlus
    Hi,

    read access violation at: 0x0, flags=0x0

    You're dereferencing a null pointer. Look at the stack at the moment of the crash and it will tell you exactly where this is happening.

    the objects init in heap store are delete by main window is some way or i have to manually delete them in destructor?

    Someone has to delete them. If you give them a parent, the parent will do it, if you don't (and you don't) you have to handle that manually.

    Kind regards.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #5

      when you click the button, plugin in the lambda will be a dangling pointer. In debug mode some compilers set dangling pointers to NULL and this triggers your error.
      Bottom line: you are misusing the lambda

      about the delete question, when you call layout->addWidget(); the layout will take care of deleting the widgets see http://doc.qt.io/qt-5/qlayout.html#addItem

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • DumaPlusPlusD Offline
        DumaPlusPlusD Offline
        DumaPlusPlus
        wrote on last edited by
        #6

        it's strange i init them in heap store and acces them with a static list:
        in mainwindow.cpp's constructor

         for(Plugin* p : PluginCollection::get_plugins())
            {
              build_tab_from_plugin(p);
            }
        

        plugincollection.h

        namespace PluginCollection
        {
          QList<Plugin *> get_plugins()
          {
            static QList<Plugin*> list {new PastebinPlugin() };
            return list;
          }
        }
        

        should static list stay in memory (as well as allocated obj) until application closing?

        1 Reply Last reply
        0
        • DumaPlusPlusD DumaPlusPlus

          of course i have this is the complete code:

          void MainWindow::build_tab_from_plugin(Plugin *plugin)
          {
            QWidget* tab = new QWidget();
          
            QVBoxLayout* layout = new QVBoxLayout();
            QPushButton* send = new QPushButton();
            QPlainTextEdit* textBox = new QPlainTextEdit();
            QLineEdit* resultBox = new QLineEdit();
          
            connect(send,&QPushButton::clicked,[&](){ resultBox->setText(plugin->send_paste(textBox->document()->toPlainText())); });
          
            resultBox->setReadOnly(true);
            send->setText("Send paste");
          
            layout->addWidget(send);
            layout->addWidget(textBox);
            layout->addWidget(resultBox);
            tab->setLayout(layout);
          
            ui->tabWidget->addTab(tab,plugin->name());
          
          }
          

          and i benefit from this thread to ask "the objects init in heap store are delete by main window is some way or i have to manually delete them in destructor?"

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #7

          @DumaPlusPlus You're passing references to local variables [&] which only exist while MainWindow::build_tab_from_plugin is being executed. Since everything you use in the lambda are pointers you can pass by value [=]. Is plugin a valid pointer?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #8

            EDIT: I WAS WRONG

            no matter how you pass thos variables [&] or [=]. when the lambda gets called all those pointers will be junk so it won't work

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            kshegunovK 1 Reply Last reply
            0
            • VRoninV VRonin

              EDIT: I WAS WRONG

              no matter how you pass thos variables [&] or [=]. when the lambda gets called all those pointers will be junk so it won't work

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #9

              @VRonin

              when the lambda gets called all those pointers will be junk

              Why? I think @jsulm is correct here.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #10

                EDIT: I WAS WRONG

                when You call build_tab_from_plugin() all the pointers are valid and fine, then the function terminates and all pointers go out of scope. when the button is pressed, the code goes into the lambda (imagine a goto), the environment of build_tab_from_plugin is recreated but you don't know what the pointers point to right now. the lambda won't save the value of any of the pointers in its body uppon declaration

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                kshegunovK jsulmJ 2 Replies Last reply
                0
                • DumaPlusPlusD Offline
                  DumaPlusPlusD Offline
                  DumaPlusPlus
                  wrote on last edited by
                  #11

                  thanks to all.
                  @jsulm resolves my problem but now i'm bit confused when do i connect SIGNAL with lambda this isn't stored somewhere? and reference used in lambda isn't an alias to a pointer (something that stay in memoery untile delete operator is called) ?

                  jsulmJ 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    EDIT: I WAS WRONG

                    when You call build_tab_from_plugin() all the pointers are valid and fine, then the function terminates and all pointers go out of scope. when the button is pressed, the code goes into the lambda (imagine a goto), the environment of build_tab_from_plugin is recreated but you don't know what the pointers point to right now. the lambda won't save the value of any of the pointers in its body uppon declaration

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #12

                    @VRonin said:

                    the lambda won't save the value of any of the pointers in its body uppon declaration

                    Sure about this? As far as I know C++11 (which isn't so well) a pointer is an automatic storage variable and is copy-captured by [=] as any other auto-storage variable.

                    the code goes into the lambda (imagine a goto), the environment of build_tab_from_plugin is recreated

                    This is very wrong way of thinking about a lambda, sorry for saying. A lambda is a typical functor (and is implemented as such).

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • DumaPlusPlusD DumaPlusPlus

                      thanks to all.
                      @jsulm resolves my problem but now i'm bit confused when do i connect SIGNAL with lambda this isn't stored somewhere? and reference used in lambda isn't an alias to a pointer (something that stay in memoery untile delete operator is called) ?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @DumaPlusPlus If you use references then then "point" to the variable. In your case they point to local variables. These local variables disappear as soon as the method finishes, so the "pointers" to them are not valid anymore - because they do not exist anymore.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • VRoninV VRonin

                        EDIT: I WAS WRONG

                        when You call build_tab_from_plugin() all the pointers are valid and fine, then the function terminates and all pointers go out of scope. when the button is pressed, the code goes into the lambda (imagine a goto), the environment of build_tab_from_plugin is recreated but you don't know what the pointers point to right now. the lambda won't save the value of any of the pointers in its body uppon declaration

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @VRonin Sorry, but you're wrong. You can easily test this: using [&] will crash, using [=] works just fine.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        VRoninV 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @VRonin Sorry, but you're wrong. You can easily test this: using [&] will crash, using [=] works just fine.

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #15

                          @jsulm I'm really confused now as I tested it and this code works 100% fine, even with [&]. how can it be?!

                          #include <QCoreApplication>
                          #include<QDebug>
                          #include <QTimer>
                          
                          
                          int main(int argc, char *argv[]) {
                              QCoreApplication appl(argc,argv);
                              QTimer mainTimer;
                              mainTimer.setSingleShot(true);
                              {
                                  QString* myString=nullptr;
                                  myString=new QString("A Message");
                                  QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << *myString;});
                                  QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << *myString;});
                                  // Memory leak!
                              }
                          
                              mainTimer.start(100);
                              return appl.exec();
                          }
                          
                          

                          EDIT:
                          Using MSVC2013 on Windows

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          kshegunovK 1 Reply Last reply
                          1
                          • VRoninV VRonin

                            @jsulm I'm really confused now as I tested it and this code works 100% fine, even with [&]. how can it be?!

                            #include <QCoreApplication>
                            #include<QDebug>
                            #include <QTimer>
                            
                            
                            int main(int argc, char *argv[]) {
                                QCoreApplication appl(argc,argv);
                                QTimer mainTimer;
                                mainTimer.setSingleShot(true);
                                {
                                    QString* myString=nullptr;
                                    myString=new QString("A Message");
                                    QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << *myString;});
                                    QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << *myString;});
                                    // Memory leak!
                                }
                            
                                mainTimer.start(100);
                                return appl.exec();
                            }
                            
                            

                            EDIT:
                            Using MSVC2013 on Windows

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #16

                            @VRonin said:

                            QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << *myString;});
                            

                            What about:

                            QObject::connect(&mainTimer,&QTimer::timeout,[&](){myString = nullptr;});
                            

                            You're capturing QString*, so you'd end up with: QString*& but you don't modify the string pointer, rather you dereference the object it's pointing to, so you'd try to output a QString & with QDebug. Also probably your compiler somewhat lax. :)

                            PS.
                            Well that's really disturbing ... I don't get any errors either. The memory will silently be overwritten. (g++ on Linux)

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #17

                              second test:

                              #include <QCoreApplication>
                              #include <QDebug>
                              #include <QTimer>
                              #include <QPointer>
                              
                              int main(int argc, char *argv[]) {
                                  QCoreApplication appl(argc,argv);
                                  QTimer mainTimer;
                                  mainTimer.setSingleShot(true);
                                  {
                                      QPointer<QObject> mybj;
                                      mybj =new QObject();
                                      mybj->setObjectName("A Message");
                                      QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << mybj->objectName(); mybj->setObjectName("Foo"); qDebug() << mybj->objectName();});
                                      QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << mybj->objectName(); mybj->setObjectName("Bar"); qDebug() << mybj->objectName();});
                                      // Memory leak!
                                  }
                                  {
                                      QString testString("Occuppy Stack");
                                  }
                                  mainTimer.start(100);
                                  return appl.exec();
                              }
                              
                              

                              Still working correctly. Notice how the first output of the [=] lambda is Foo. HOW?!

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              DumaPlusPlusD kshegunovK 2 Replies Last reply
                              0
                              • VRoninV VRonin

                                second test:

                                #include <QCoreApplication>
                                #include <QDebug>
                                #include <QTimer>
                                #include <QPointer>
                                
                                int main(int argc, char *argv[]) {
                                    QCoreApplication appl(argc,argv);
                                    QTimer mainTimer;
                                    mainTimer.setSingleShot(true);
                                    {
                                        QPointer<QObject> mybj;
                                        mybj =new QObject();
                                        mybj->setObjectName("A Message");
                                        QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << mybj->objectName(); mybj->setObjectName("Foo"); qDebug() << mybj->objectName();});
                                        QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << mybj->objectName(); mybj->setObjectName("Bar"); qDebug() << mybj->objectName();});
                                        // Memory leak!
                                    }
                                    {
                                        QString testString("Occuppy Stack");
                                    }
                                    mainTimer.start(100);
                                    return appl.exec();
                                }
                                
                                

                                Still working correctly. Notice how the first output of the [=] lambda is Foo. HOW?!

                                DumaPlusPlusD Offline
                                DumaPlusPlusD Offline
                                DumaPlusPlus
                                wrote on last edited by DumaPlusPlus
                                #18

                                @VRonin said:

                                second test:

                                        QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << mybj->objectName(); mybj->setObjectName("Foo"); qDebug() << mybj->objectName();});
                                        QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << mybj->objectName(); mybj->setObjectName("Bar"); qDebug() << mybj->objectName();});
                                
                                
                                Still working correctly. Notice how the first output of the [=] lambda is Foo. HOW?!
                                

                                should be that?
                                passing by value copy the pointer so you point to objname with modified name...right?

                                VRoninV 1 Reply Last reply
                                0
                                • DumaPlusPlusD DumaPlusPlus

                                  @VRonin said:

                                  second test:

                                          QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << mybj->objectName(); mybj->setObjectName("Foo"); qDebug() << mybj->objectName();});
                                          QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << mybj->objectName(); mybj->setObjectName("Bar"); qDebug() << mybj->objectName();});
                                  
                                  
                                  Still working correctly. Notice how the first output of the [=] lambda is Foo. HOW?!
                                  

                                  should be that?
                                  passing by value copy the pointer so you point to objname with modified name...right?

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #19

                                  @DumaPlusPlus The first lambda should never be executed at the creation of the second one. then when the timer times out I was expecting the first to crash or operate on invalid memory and the second to print

                                  A Message
                                  Bar
                                  

                                  While it looks like the lambda with [&] behaves exactly as the one with [=]

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    second test:

                                    #include <QCoreApplication>
                                    #include <QDebug>
                                    #include <QTimer>
                                    #include <QPointer>
                                    
                                    int main(int argc, char *argv[]) {
                                        QCoreApplication appl(argc,argv);
                                        QTimer mainTimer;
                                        mainTimer.setSingleShot(true);
                                        {
                                            QPointer<QObject> mybj;
                                            mybj =new QObject();
                                            mybj->setObjectName("A Message");
                                            QObject::connect(&mainTimer,&QTimer::timeout,[&](){qDebug() << mybj->objectName(); mybj->setObjectName("Foo"); qDebug() << mybj->objectName();});
                                            QObject::connect(&mainTimer,&QTimer::timeout,[=](){qDebug() << mybj->objectName(); mybj->setObjectName("Bar"); qDebug() << mybj->objectName();});
                                            // Memory leak!
                                        }
                                        {
                                            QString testString("Occuppy Stack");
                                        }
                                        mainTimer.start(100);
                                        return appl.exec();
                                    }
                                    
                                    

                                    Still working correctly. Notice how the first output of the [=] lambda is Foo. HOW?!

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by kshegunov
                                    #20

                                    @VRonin

                                    If I haven't missed anything. my g++ doesn't open a new stack frame when it sees: { }. So in your examples (which I used) all is flat, thus myString is in main()'s stack frame, which means it doesn't go out of scope (i.e. it's not pop-ed from the stack), which ultimately means that the lambda capture is valid.

                                    Here's what I have for main() from your QString test case:

                                    # Sets up main()'s stack
                                    0x400fc2                   55                                push   %rbp
                                    0x400fc3  <+0x0001>        48 89 e5                          mov    %rsp,%rbp
                                    ...
                                    0x400fcf  <+0x000d>        48 83 ec 78                       sub    $0x78,%rsp
                                    ...
                                    # mainTimer.setSingleShot(true);
                                    0x401010  <+0x004e>        48 8d 45 80                       lea    -0x80(%rbp),%rax
                                    0x401014  <+0x0052>        be 01 00 00 00                    mov    $0x1,%esi
                                    0x401019  <+0x0057>        48 89 c7                          mov    %rax,%rdi
                                    0x40101c  <+0x005a>        e8 59 08 00 00                    callq  0x40187a <QTimer::setSingleShot(bool)>
                                    # QString * myString = nullptr;
                                    0x401021  <+0x005f>        48 c7 85 78 ff ff ff 00 00 00 00  movq   $0x0,-0x88(%rbp)
                                    # No stack frame was opened as one'd expect from a block
                                    ...
                                    # main()'s stack's being unwound
                                    0x40113c  <+0x017a>        48 83 c4 78                       add    $0x78,%rsp
                                    ...
                                    0x401149  <+0x0187>        5d                                pop    %rbp
                                    # And that was all folks, thanks for playing
                                    0x40114a  <+0x0188>        c3                                retq         
                                    

                                    As for the lambda, it doesn't make any checks. It just stores the captured address (the reference) and ultimately dereferences it when it's executed:

                                    ...
                                    # qDebug() << *myString;
                                    0x400ec3  <+0x000d>        48 8b 45 b8           mov    -0x48(%rbp),%rax    # Load QString *& from the base pointer
                                    0x400ec7  <+0x0011>        48 8b 00              mov    (%rax),%rax         # Dereference once (strip &)
                                    0x400eca  <+0x0014>        48 8b 18              mov    (%rax),%rbx         # Dereference second time i.e. (*myString)
                                    

                                    So I hope this explains how and why.

                                    Kind regards.

                                    Read and abide by the Qt Code of Conduct

                                    VRoninV 1 Reply Last reply
                                    1
                                    • kshegunovK kshegunov

                                      @VRonin

                                      If I haven't missed anything. my g++ doesn't open a new stack frame when it sees: { }. So in your examples (which I used) all is flat, thus myString is in main()'s stack frame, which means it doesn't go out of scope (i.e. it's not pop-ed from the stack), which ultimately means that the lambda capture is valid.

                                      Here's what I have for main() from your QString test case:

                                      # Sets up main()'s stack
                                      0x400fc2                   55                                push   %rbp
                                      0x400fc3  <+0x0001>        48 89 e5                          mov    %rsp,%rbp
                                      ...
                                      0x400fcf  <+0x000d>        48 83 ec 78                       sub    $0x78,%rsp
                                      ...
                                      # mainTimer.setSingleShot(true);
                                      0x401010  <+0x004e>        48 8d 45 80                       lea    -0x80(%rbp),%rax
                                      0x401014  <+0x0052>        be 01 00 00 00                    mov    $0x1,%esi
                                      0x401019  <+0x0057>        48 89 c7                          mov    %rax,%rdi
                                      0x40101c  <+0x005a>        e8 59 08 00 00                    callq  0x40187a <QTimer::setSingleShot(bool)>
                                      # QString * myString = nullptr;
                                      0x401021  <+0x005f>        48 c7 85 78 ff ff ff 00 00 00 00  movq   $0x0,-0x88(%rbp)
                                      # No stack frame was opened as one'd expect from a block
                                      ...
                                      # main()'s stack's being unwound
                                      0x40113c  <+0x017a>        48 83 c4 78                       add    $0x78,%rsp
                                      ...
                                      0x401149  <+0x0187>        5d                                pop    %rbp
                                      # And that was all folks, thanks for playing
                                      0x40114a  <+0x0188>        c3                                retq         
                                      

                                      As for the lambda, it doesn't make any checks. It just stores the captured address (the reference) and ultimately dereferences it when it's executed:

                                      ...
                                      # qDebug() << *myString;
                                      0x400ec3  <+0x000d>        48 8b 45 b8           mov    -0x48(%rbp),%rax    # Load QString *& from the base pointer
                                      0x400ec7  <+0x0011>        48 8b 00              mov    (%rax),%rax         # Dereference once (strip &)
                                      0x400eca  <+0x0014>        48 8b 18              mov    (%rax),%rbx         # Dereference second time i.e. (*myString)
                                      

                                      So I hope this explains how and why.

                                      Kind regards.

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #21

                                      Thanks @kshegunov now it make sense, it was just the compiler (I use MSVC btw) optimizing.
                                      This behaves as expected.

                                      #include <QCoreApplication>
                                      #include<QDebug>
                                      #include <QTimer>
                                      
                                      void makeConnections(QTimer* mainTimer ){
                                          QString* myString=nullptr;
                                          myString=new QString("A Message");
                                          QObject::connect(mainTimer,&QTimer::timeout,[&](){qDebug() << *myString;});
                                          QObject::connect(mainTimer,&QTimer::timeout,[=](){qDebug() << *myString;});
                                      }
                                      
                                      int main(int argc, char *argv[]) {
                                          QCoreApplication appl(argc,argv);
                                          QTimer mainTimer;
                                          mainTimer.setSingleShot(true);
                                          makeConnections(&mainTimer);
                                      
                                          mainTimer.start(100);
                                          return appl.exec();
                                      }
                                      
                                      

                                      I marked my previous post where I was wrong and the final answer to the topic is use [=] in the lambda instead of [&]

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      kshegunovK 1 Reply Last reply
                                      0
                                      • VRoninV VRonin

                                        Thanks @kshegunov now it make sense, it was just the compiler (I use MSVC btw) optimizing.
                                        This behaves as expected.

                                        #include <QCoreApplication>
                                        #include<QDebug>
                                        #include <QTimer>
                                        
                                        void makeConnections(QTimer* mainTimer ){
                                            QString* myString=nullptr;
                                            myString=new QString("A Message");
                                            QObject::connect(mainTimer,&QTimer::timeout,[&](){qDebug() << *myString;});
                                            QObject::connect(mainTimer,&QTimer::timeout,[=](){qDebug() << *myString;});
                                        }
                                        
                                        int main(int argc, char *argv[]) {
                                            QCoreApplication appl(argc,argv);
                                            QTimer mainTimer;
                                            mainTimer.setSingleShot(true);
                                            makeConnections(&mainTimer);
                                        
                                            mainTimer.start(100);
                                            return appl.exec();
                                        }
                                        
                                        

                                        I marked my previous post where I was wrong and the final answer to the topic is use [=] in the lambda instead of [&]

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by kshegunov
                                        #22

                                        @VRonin

                                        Thanks @kshegunov now it make sense, it was just the compiler (I use MSVC btw) optimizing.

                                        No problem. Yes the compiler was an issue apparently, although that's some strange optimization made. Especially if you take into account we're running in debug mode, two different compilers no less. But even in release mode I'd venture to say one doesn't expect a block to just be ignored ... at least I don't.

                                        This behaves as expected.

                                        Meaning it crashes at the appropriate place? :)

                                        Read and abide by the Qt Code of Conduct

                                        VRoninV 1 Reply Last reply
                                        0
                                        • kshegunovK kshegunov

                                          @VRonin

                                          Thanks @kshegunov now it make sense, it was just the compiler (I use MSVC btw) optimizing.

                                          No problem. Yes the compiler was an issue apparently, although that's some strange optimization made. Especially if you take into account we're running in debug mode, two different compilers no less. But even in release mode I'd venture to say one doesn't expect a block to just be ignored ... at least I don't.

                                          This behaves as expected.

                                          Meaning it crashes at the appropriate place? :)

                                          VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by
                                          #23

                                          @kshegunov said:

                                          Meaning it crashes at the appropriate place? :)

                                          Even a crash sometimes is expected behaviour ;)

                                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                          ~Napoleon Bonaparte

                                          On a crusade to banish setIndexWidget() from the holy land of Qt

                                          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