Segmentation fault when calling 3 methods, works normally when calling only 2 of same methods
-
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!
-
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!
@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.
-
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!
@eljefe711
And you don't state anywhere what typeBriefUserModelis? -
@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.
@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 0x7ff66d5e14c6As 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 typeBriefUserModelis?BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.
-
@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 0x7ff66d5e14c6As 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 typeBriefUserModelis?BriefUserModel is a class that I've created, and the "convertUserListToModel()" method, where the problem arises, is its member function.
@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);inBriefUserModel::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.
-
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!
@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?
-
@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);inBriefUserModel::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.
@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);inBriefUserModel::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. -
E eljefe711 has marked this topic as solved on
-
@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?
@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-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! :)
@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.