简体   繁体   English

Qt分割故障

[英]Qt segmentation fault

I must develop a social network simulator using C++ and QT library. 我必须使用C ++和QT库开发一个社交网络模拟器。 I store user into mysql database using QODBC. 我使用QODBC将用户存储到mysql数据库中。 When i run my application, i throw a SIGSEGV error. 当我运行我的应用程序时,我抛出了SIGSEGV错误。

here, my function which throw this error : 在这里,我的函数抛出此错误:

QMutex userMutex;
userMutex.lock();

QListIterator<User*> i(users);
User* user;
QString sql = "insert into t_user (id, pseudo, name, firstname, birthdate) values ";
QString bindValue = QString::fromStdString("(?, ?, ?, ?, ?),").repeated(users.count());
sql.append(bindValue);
QSqlQuery query = QSqlQuery(Interface::getCnx());
query.prepare(sql);
while(i.hasNext())
{
    user = i.next();
    query.addBindValue(QString::number(user->getId()));
    query.addBindValue(user->getPseudo());
    query.addBindValue(user->getName());
    query.addBindValue(user->getFirstname());
    QString birthdate = QString::number(user->getBirthDate().year()) + "-" + QString::number(user->getBirthDate().month()) + "-" + QString::number(user->getBirthDate().day());
    query.addBindValue(birthdate);
}
query.exec();

userMutex.unlock();

It is "query.exec()" line which throw this error. 是“ query.exec()”行引发此错误。

Do you see what's wrong? 看到什么地方了吗

First of all you have an extra comma here: 首先,您在这里有一个逗号:

QString bindValue = QString::fromStdString("(?, ?, ?, ?, ?),").repeated(users.count());

So or you "repeat" string count-1 times and then add "(?, ?, ?, ?, ?)": 因此,或者您“重复”字符串计数1次,然后添加“(?,?,?,?,?)”:

QString bindValue = QString::fromStdString("(?, ?, ?, ?, ?),").repeated(users.count() - 1);
bindValue "(?, ?, ?, ?, ?)";

Or you have to remove last char of your string bindValue. 或者,您必须删除字符串bindValue的最后一个字符。

I found the answer ... i guess. 我找到了答案...我想。 This SIGSEGV error has disappeared when i decrease the size of the list. 当我减小列表的大小时,此SIGSEGV错误已消失。 Before the list contains 1000 users but now it constains only 800 users per call to this méthod. 该列表之前包含1000个用户,但现在每次调用此方法仅包含800个用户。

Thanks for your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM