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. Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods
Forum Updated to NodeBB v4.3 + New Features

Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 496 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.
  • E Offline
    E Offline
    eljefe711
    wrote on last edited by eljefe711
    #1

    Hello, I just recently started learning Qt and I ran into a roadblock today trying to make a model for my TableView.

    The TableView is supposed to have 3 columns: First Name, Last Name and Email. Firstly, I create several <User> objects and put them inside of a QVector, which I pass to the method that makes the model.

    Inside that method, I iterate through the vector and grab the First Name, Last Name and Email from the iterated User object using its methods getName(), getLastName() and getEmail(), respectively. Below you can see the User class:

    User::User(const QString name, const QString lastName, const QString email)
    {
        this->name = name;
        this->lastName = lastName;
        this->email = email;
    }
    
    User::~User()
    {
    
    }
    
    QString User::getName() const
    {
        return this->name;
    }
    
    QString User::getLastName() const
    {
        return this->lastName;
    }
    
    QString User::getEmail() const
    {
        return this->email;
    }
    

    After that, those 3 properties are supposed to be appended into a QList, which is then appended into the model. Below is the code that's supposed to append the Users into the model data:

    void BriefUserModel::convertUserListToModel()
    {
        QVector<User> userList = this->json.getUsersBrief();
        QVectorIterator<User> it(userList);
    
        while(it.hasNext())
        {
            QString name = it.next().getName();
            QString lastName = it.next().getLastName();
            QString email = it.next().getEmail();
    
            QList<QString> modelRow;
            modelRow.append(name);
            modelRow.append(lastName);
            modelRow.append(email);
    
            this->append(modelRow);
            this->reset();
        }
    }
    

    And obviously, in my main class, I created a TableView and a Model object, set its column number to 3 and whenever I try setting the model, the application just crashes. Below is the code that sets up the TableView:

    QTableView *briefDetailsTableView = new QTableView();
    briefDetailsTableView->setObjectName("briefDetailsTableView");
    BriefUserModel *bfm = new BriefUserModel(3);
    bfm->convertUserListToModel();
    briefDetailsTableView->setModel(bfm);
    usersLayout->addWidget(briefDetailsTableView);
    

    Now here's where it gets tricky.
    When I run the program without debug, it just crashes. If I run it with debug, it says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

    If I comment out that method and the appending of the email below it and only leave First and Last Name, as well as change the number of columns from 3 to 2, everything works normally and the TableView displays the two columns.
    More so, if I comment out any of the 3 properties I need and leave only two, it will work.

    I tried changing the placements of when each method is called (e.g. I put getEmail() first, getName() second and getLastName() third) and it's always at the place of the third method where the segmentation fault occurs (in the mentioned case, it occurs when getLastName() is called).

    I've read that segmentation faults are linked to null pointers, but I have no idea how that relates to my case since I don't work with pointers at all except for the dynamically allocated model, which isn't even where the fault occurs.

    Any help would be greatly appreciated!

    Christian EhrlicherC JonBJ 3 Replies Last reply
    0
    • E eljefe711

      Hello, I just recently started learning Qt and I ran into a roadblock today trying to make a model for my TableView.

      The TableView is supposed to have 3 columns: First Name, Last Name and Email. Firstly, I create several <User> objects and put them inside of a QVector, which I pass to the method that makes the model.

      Inside that method, I iterate through the vector and grab the First Name, Last Name and Email from the iterated User object using its methods getName(), getLastName() and getEmail(), respectively. Below you can see the User class:

      User::User(const QString name, const QString lastName, const QString email)
      {
          this->name = name;
          this->lastName = lastName;
          this->email = email;
      }
      
      User::~User()
      {
      
      }
      
      QString User::getName() const
      {
          return this->name;
      }
      
      QString User::getLastName() const
      {
          return this->lastName;
      }
      
      QString User::getEmail() const
      {
          return this->email;
      }
      

      After that, those 3 properties are supposed to be appended into a QList, which is then appended into the model. Below is the code that's supposed to append the Users into the model data:

      void BriefUserModel::convertUserListToModel()
      {
          QVector<User> userList = this->json.getUsersBrief();
          QVectorIterator<User> it(userList);
      
          while(it.hasNext())
          {
              QString name = it.next().getName();
              QString lastName = it.next().getLastName();
              QString email = it.next().getEmail();
      
              QList<QString> modelRow;
              modelRow.append(name);
              modelRow.append(lastName);
              modelRow.append(email);
      
              this->append(modelRow);
              this->reset();
          }
      }
      

      And obviously, in my main class, I created a TableView and a Model object, set its column number to 3 and whenever I try setting the model, the application just crashes. Below is the code that sets up the TableView:

      QTableView *briefDetailsTableView = new QTableView();
      briefDetailsTableView->setObjectName("briefDetailsTableView");
      BriefUserModel *bfm = new BriefUserModel(3);
      bfm->convertUserListToModel();
      briefDetailsTableView->setModel(bfm);
      usersLayout->addWidget(briefDetailsTableView);
      

      Now here's where it gets tricky.
      When I run the program without debug, it just crashes. If I run it with debug, it says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

      If I comment out that method and the appending of the email below it and only leave First and Last Name, as well as change the number of columns from 3 to 2, everything works normally and the TableView displays the two columns.
      More so, if I comment out any of the 3 properties I need and leave only two, it will work.

      I tried changing the placements of when each method is called (e.g. I put getEmail() first, getName() second and getLastName() third) and it's always at the place of the third method where the segmentation fault occurs (in the mentioned case, it occurs when getLastName() is called).

      I've read that segmentation faults are linked to null pointers, but I have no idea how that relates to my case since I don't work with pointers at all except for the dynamically allocated model, which isn't even where the fault occurs.

      Any help would be greatly appreciated!

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

      @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

      t says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

      Then look at the backtrace to see where it comes from and fix your code accordingly.

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

      E 1 Reply Last reply
      0
      • E eljefe711

        Hello, I just recently started learning Qt and I ran into a roadblock today trying to make a model for my TableView.

        The TableView is supposed to have 3 columns: First Name, Last Name and Email. Firstly, I create several <User> objects and put them inside of a QVector, which I pass to the method that makes the model.

        Inside that method, I iterate through the vector and grab the First Name, Last Name and Email from the iterated User object using its methods getName(), getLastName() and getEmail(), respectively. Below you can see the User class:

        User::User(const QString name, const QString lastName, const QString email)
        {
            this->name = name;
            this->lastName = lastName;
            this->email = email;
        }
        
        User::~User()
        {
        
        }
        
        QString User::getName() const
        {
            return this->name;
        }
        
        QString User::getLastName() const
        {
            return this->lastName;
        }
        
        QString User::getEmail() const
        {
            return this->email;
        }
        

        After that, those 3 properties are supposed to be appended into a QList, which is then appended into the model. Below is the code that's supposed to append the Users into the model data:

        void BriefUserModel::convertUserListToModel()
        {
            QVector<User> userList = this->json.getUsersBrief();
            QVectorIterator<User> it(userList);
        
            while(it.hasNext())
            {
                QString name = it.next().getName();
                QString lastName = it.next().getLastName();
                QString email = it.next().getEmail();
        
                QList<QString> modelRow;
                modelRow.append(name);
                modelRow.append(lastName);
                modelRow.append(email);
        
                this->append(modelRow);
                this->reset();
            }
        }
        

        And obviously, in my main class, I created a TableView and a Model object, set its column number to 3 and whenever I try setting the model, the application just crashes. Below is the code that sets up the TableView:

        QTableView *briefDetailsTableView = new QTableView();
        briefDetailsTableView->setObjectName("briefDetailsTableView");
        BriefUserModel *bfm = new BriefUserModel(3);
        bfm->convertUserListToModel();
        briefDetailsTableView->setModel(bfm);
        usersLayout->addWidget(briefDetailsTableView);
        

        Now here's where it gets tricky.
        When I run the program without debug, it just crashes. If I run it with debug, it says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

        If I comment out that method and the appending of the email below it and only leave First and Last Name, as well as change the number of columns from 3 to 2, everything works normally and the TableView displays the two columns.
        More so, if I comment out any of the 3 properties I need and leave only two, it will work.

        I tried changing the placements of when each method is called (e.g. I put getEmail() first, getName() second and getLastName() third) and it's always at the place of the third method where the segmentation fault occurs (in the mentioned case, it occurs when getLastName() is called).

        I've read that segmentation faults are linked to null pointers, but I have no idea how that relates to my case since I don't work with pointers at all except for the dynamically allocated model, which isn't even where the fault occurs.

        Any help would be greatly appreciated!

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

        @eljefe711
        And you don't state anywhere what type BriefUserModel is?

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

          t says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

          Then look at the backtrace to see where it comes from and fix your code accordingly.

          E Offline
          E Offline
          eljefe711
          wrote on last edited by eljefe711
          #4

          @Christian-Ehrlicher said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

          @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

          t says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

          Then look at the backtrace to see where it comes from and fix your code accordingly.

          This is how my backtrace looks like:

          1 User::getEmail() const                                           0x7ff66d5e5d4c 
          2 BriefUserModel::convertUserListToModel()                         0x7ff66d5e1c97 
          3 GUI::makeGUI(QWidget&)                                           0x7ff66d5e24b4 
          4 qMain(int, char * *)                                             0x7ff66d5e59c5 
          5 WinMain                                  qtentrypoint_win.cpp 50 0x7ff66d5e74f2 
          6 __tmainCRTStartup                                                0x7ff66d5e1395 
          7 WinMainCRTStartup                                                0x7ff66d5e14c6 
          

          As you can see, it says that the segfault is in win.cpp at the 50th line, but that everything starts at the getEmail() method. I have no idea what to fix since there is only one command in that method and that's to return the value. Also I'm sure it's not the actual method that's causing the problem because it works if I put it before the other two, but rather something inside that iteration, I just can't figure out what.

          @JonB said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

          @eljefe711
          And you don't state anywhere what type BriefUserModel is?

          BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.

          JonBJ 1 Reply Last reply
          0
          • E eljefe711

            @Christian-Ehrlicher said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

            @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

            t says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

            Then look at the backtrace to see where it comes from and fix your code accordingly.

            This is how my backtrace looks like:

            1 User::getEmail() const                                           0x7ff66d5e5d4c 
            2 BriefUserModel::convertUserListToModel()                         0x7ff66d5e1c97 
            3 GUI::makeGUI(QWidget&)                                           0x7ff66d5e24b4 
            4 qMain(int, char * *)                                             0x7ff66d5e59c5 
            5 WinMain                                  qtentrypoint_win.cpp 50 0x7ff66d5e74f2 
            6 __tmainCRTStartup                                                0x7ff66d5e1395 
            7 WinMainCRTStartup                                                0x7ff66d5e14c6 
            

            As you can see, it says that the segfault is in win.cpp at the 50th line, but that everything starts at the getEmail() method. I have no idea what to fix since there is only one command in that method and that's to return the value. Also I'm sure it's not the actual method that's causing the problem because it works if I put it before the other two, but rather something inside that iteration, I just can't figure out what.

            @JonB said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

            @eljefe711
            And you don't state anywhere what type BriefUserModel is?

            BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.

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

            @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

            BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.

            This is not an (acceptable/sought) answer. How can we assess what your this->append(modelRow); in BriefUserModel::convertUserListToModel() does from this description?

            Compile with debug for your backtrace to convey more useful information. It should be showing source code and allow you to examine variables when it crashes in debugger.

            E 1 Reply Last reply
            1
            • E eljefe711

              Hello, I just recently started learning Qt and I ran into a roadblock today trying to make a model for my TableView.

              The TableView is supposed to have 3 columns: First Name, Last Name and Email. Firstly, I create several <User> objects and put them inside of a QVector, which I pass to the method that makes the model.

              Inside that method, I iterate through the vector and grab the First Name, Last Name and Email from the iterated User object using its methods getName(), getLastName() and getEmail(), respectively. Below you can see the User class:

              User::User(const QString name, const QString lastName, const QString email)
              {
                  this->name = name;
                  this->lastName = lastName;
                  this->email = email;
              }
              
              User::~User()
              {
              
              }
              
              QString User::getName() const
              {
                  return this->name;
              }
              
              QString User::getLastName() const
              {
                  return this->lastName;
              }
              
              QString User::getEmail() const
              {
                  return this->email;
              }
              

              After that, those 3 properties are supposed to be appended into a QList, which is then appended into the model. Below is the code that's supposed to append the Users into the model data:

              void BriefUserModel::convertUserListToModel()
              {
                  QVector<User> userList = this->json.getUsersBrief();
                  QVectorIterator<User> it(userList);
              
                  while(it.hasNext())
                  {
                      QString name = it.next().getName();
                      QString lastName = it.next().getLastName();
                      QString email = it.next().getEmail();
              
                      QList<QString> modelRow;
                      modelRow.append(name);
                      modelRow.append(lastName);
                      modelRow.append(email);
              
                      this->append(modelRow);
                      this->reset();
                  }
              }
              

              And obviously, in my main class, I created a TableView and a Model object, set its column number to 3 and whenever I try setting the model, the application just crashes. Below is the code that sets up the TableView:

              QTableView *briefDetailsTableView = new QTableView();
              briefDetailsTableView->setObjectName("briefDetailsTableView");
              BriefUserModel *bfm = new BriefUserModel(3);
              bfm->convertUserListToModel();
              briefDetailsTableView->setModel(bfm);
              usersLayout->addWidget(briefDetailsTableView);
              

              Now here's where it gets tricky.
              When I run the program without debug, it just crashes. If I run it with debug, it says that the segmentation error occurred at the User::getEmail() method, which is called during the data collection for the model.

              If I comment out that method and the appending of the email below it and only leave First and Last Name, as well as change the number of columns from 3 to 2, everything works normally and the TableView displays the two columns.
              More so, if I comment out any of the 3 properties I need and leave only two, it will work.

              I tried changing the placements of when each method is called (e.g. I put getEmail() first, getName() second and getLastName() third) and it's always at the place of the third method where the segmentation fault occurs (in the mentioned case, it occurs when getLastName() is called).

              I've read that segmentation faults are linked to null pointers, but I have no idea how that relates to my case since I don't work with pointers at all except for the dynamically allocated model, which isn't even where the fault occurs.

              Any help would be greatly appreciated!

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

              @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                  QString name = it.next().getName();
                  QString lastName = it.next().getLastName();
                  QString email = it.next().getEmail();
              

              You're aware what it.next() is doing?

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

              JonBJ 1 Reply Last reply
              3
              • JonBJ JonB

                @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.

                This is not an (acceptable/sought) answer. How can we assess what your this->append(modelRow); in BriefUserModel::convertUserListToModel() does from this description?

                Compile with debug for your backtrace to convey more useful information. It should be showing source code and allow you to examine variables when it crashes in debugger.

                E Offline
                E Offline
                eljefe711
                wrote on last edited by
                #7

                @JonB said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.

                This is not an (acceptable/sought) answer. How can we assess what your this->append(modelRow); in BriefUserModel::convertUserListToModel() does from this description?

                Compile with debug for your backtrace to convey more useful information. It should be showing source code and allow you to examine variables when it crashes in debugger.

                void BriefUserModel::append(const QList<QString>& data)
                {
                    items.append(data); //We append data by adding a Qlist of Qstrings (data from each column that form a row)
                }
                

                "items" is a member variable inside BriefUserModel. Its type is QVector<QList<QString>>, which is why I append a QList<QString> inside the "append" method.

                @Christian-Ehrlicher said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                    QString name = it.next().getName();
                    QString lastName = it.next().getLastName();
                    QString email = it.next().getEmail();
                

                You're aware what it.next() is doing?

                This question however made me realize how much of a dumbass I was. I changed the code to:

                User currentUser = it.next();
                QString name = currentUser.getName();
                QString lastName = currentUser.getLastName();
                QString email = currentUser.getEmail();
                

                And now everything's working properly!
                I wasn't aware that the next() method moves the pointer every time it's called, so thanks for the heads up.

                1 Reply Last reply
                0
                • E eljefe711 has marked this topic as solved on
                • Christian EhrlicherC Christian Ehrlicher

                  @eljefe711 said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                      QString name = it.next().getName();
                      QString lastName = it.next().getLastName();
                      QString email = it.next().getEmail();
                  

                  You're aware what it.next() is doing?

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

                  @Christian-Ehrlicher said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                  You're aware what it.next() is doing?

                  LOL, well spotted! :)

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Christian-Ehrlicher said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                    You're aware what it.next() is doing?

                    LOL, well spotted! :)

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

                    @JonB said in Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods:

                    LOL, well spotted! :)

                    But only at the second sight... I overlooked it during the first reading because I normally don't use these iterators. It's much easier to read and write it this way:

                    const QVector<User> userList = json.getUsersBrief();
                    for (const aut&user : userList)
                      append({user.getName(), user.getLastName(), user.getEmail()});
                    
                    reset();  // whyever this is needed, begin/endInsertRows() would suffice here.
                    

                    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
                    1

                    • Login

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