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. Crashing when accessing class within lambda
Forum Updated to NodeBB v4.3 + New Features

Crashing when accessing class within lambda

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 737 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.
  • G Offline
    G Offline
    G_ka
    wrote on last edited by G_ka
    #1

    Hello,
    I would need some help to understand lambda functions. I get inconsistent SegFaults : when ran in GDB, my program always crashes. When ran outside of GDB, it sometimes works.
    Here is my code :

    connect(pathButton, &QPushButton::pressed, this,
    [=, &optimiser](){
            QString dir = QFileDialog::getExistingDirectory(this, "Open Directory", optimiser.getmodPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
            modpathTextEdit->setPlainText(dir);
            optimiser.setModPath(dir);
        });
    

    If I remove optimiser.getmodPath(), it doesn't crash.

    QString Optimiser::getmodPath() const
    {
        return modPath;
    }
    

    It shouldn't be necessary, but here is my full code.

    Thanks.

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

      Hi and welcome to the forums
      I could not spot same code in gitlab ?

      I was wondering if the optimiser you capture by reference will stay in scope ?

      1 Reply Last reply
      1
      • G Offline
        G Offline
        G_ka
        wrote on last edited by
        #3

        Sorry, I linked the wrong branch. It is here.

        The optimiser is declared in mainWindow, so it should stay in scope as far as I know.

        mrjjM 1 Reply Last reply
        0
        • G G_ka

          Sorry, I linked the wrong branch. It is here.

          The optimiser is declared in mainWindow, so it should stay in scope as far as I know.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @G_ka
          Hi
          Aha :)
          It seems to be defined as a local variable in constructor

          MainWindow::MainWindow()
          {
          Optimiser optimiser("C:/", mw_log); // local variable

          so since you capture it with reference, it will point to a deleted variable as
          constructor has run and finished once you press button.

          Put it as a class member of MainWindow (in .h )
          and it will work fine.

          1 Reply Last reply
          5
          • G G_ka

            Hello,
            I would need some help to understand lambda functions. I get inconsistent SegFaults : when ran in GDB, my program always crashes. When ran outside of GDB, it sometimes works.
            Here is my code :

            connect(pathButton, &QPushButton::pressed, this,
            [=, &optimiser](){
                    QString dir = QFileDialog::getExistingDirectory(this, "Open Directory", optimiser.getmodPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
                    modpathTextEdit->setPlainText(dir);
                    optimiser.setModPath(dir);
                });
            

            If I remove optimiser.getmodPath(), it doesn't crash.

            QString Optimiser::getmodPath() const
            {
                return modPath;
            }
            

            It shouldn't be necessary, but here is my full code.

            Thanks.

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

            @G_ka
            as an additional note,

            you can give your connect statement an 4th parameter, for example optimiser, if it's an QObject-derived class -

            Thatway the connection will get resolved when either the sender object(pathButton) or the reference object becomes invalid (optimiser).

            connect(pathButton, &QPushButton::pressed,  &optimiser,
            [=, &optimiser](){
                    QString dir = QFileDialog::getExistingDirectory(this, "Open Directory", optimiser.getmodPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
                    modpathTextEdit->setPlainText(dir);
                    optimiser.setModPath(dir);
                });
            

            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
            6
            • G Offline
              G Offline
              G_ka
              wrote on last edited by G_ka
              #6

              Thank you, it worked. @J-Hilk I'll remember it.

              Now, I have another issue. When I append anything to the log, like here, I crash.

              It isn't related to Qt tho, and I probably should be able to fix it by myself. Thanks for the help already.

              Edit : I've fixed it, I was giving the argument before it was initialized.

              1 Reply Last reply
              2

              • Login

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