读取QTextStream时出现段错误

时间:2014-07-02 11:36:23

标签: qt qtextstream

上下文: 我有一组这样的文件:

    alias
USER = usr_name
PASSWORD = pass
...

    alias2
...

我有一个组合框来选择在qtableview中显示哪个文件。下面是当组合框的值改变时调用的插槽。

void paramTableModel::switchTable(QString env)
{
    // is there an environment defined
    // --------------------------------------
    if(env.compare("") != 0)
    {
        // does the param file exist ?
        // ---------------------------
        QString file_path = "/path/to/" + env + "/path/to/file;
        QFile param_file(file_path);
        if( param_file.exists() && param_file.open(QIODevice::ReadOnly))
        {

            QString program_decrypt = "/path/to/decrypt";
            QStringList args;
            args << file_path;

            QProcess* process = new QProcess();
            process->setReadChannel(QProcess::StandardOutput);
            process->start(program_decrypt,args);
            process->waitForFinished(-1);

            QTextStream in(&process->readAll());

            //start resetting model (notify the view )
            beginResetModel();
            this->table.clear();
            std::set<QString> uniq;

            // parse file to model
            // -------------------
            while( !in.atEnd() )
            {
                QString line = in.readLine();

                // some new UT ?
                // -------------------
                if( !line.isNull() && line.trimmed().compare("") != 0 && !line.contains("="))
                {
                    line = line.trimmed();
                    std::vector<QString> row;
                    if(uniq.find(line) == uniq.end())
                    {
                        uniq.insert(line);
                        row.push_back(line);

                        QString user_line  = in.readLine().trimmed();
                        QString password_line  = in.readLine().trimmed();
                        QString server_line  = in.readLine().trimmed();
                        if( user_line.startsWith("USER") )
                        {
                            user_line = user_line.split('=').at(1).trimmed();
                        }
                        if( password_line.startsWith("PASSWORD") )
                        {
                            password_line = password_line.split('=').at(1).trimmed();
                        }
                        if( server_line.startsWith("SERVER") )
                        {
                            server_line = server_line.split('=').at(1).trimmed();
                        }
                        row.push_back(user_line);
                        row.push_back(password_line);
                        row.push_back(server_line);
                        this->table.push_back(row);
                    }
                    else
                    {
                            QMessageBox msgBox;
                            msgBox.setText(QString("%1 is already defined, ingnoring").arg(line));
                            msgBox.exec();
                    }
                }
            }
            param_file.close();
            endResetModel();
            delete process;
        }
    }
}

大部分时间,我的观点都已正确刷新。并且不时,这一行段错误:

QString line = in.readLine();

我没有找到以系统方式重现错误的模式。我可以在组合框中的两个文件之间来回点击,它将工作十次,十二次,二十次然后是段错误。

修改

这确实有效。

void paramTableModel::switchTable(QString env)
{
    // is there an environment defined
    // --------------------------------------
    if(env.isEmpty())
        return;

    // does the param file exist ?
    // --------------------------------
    QString file_path = "/path/to/env/" + env + "/path/to/file.dat";
    QFile param_file(file_path);
    if( param_file.exists() && param_file.open(QIODevice::ReadOnly))
    {

        // Call QProcess, fx_decrypt, read standard output
        QString program_decrypt = "/path/to/decrypt";
        QStringList args;
        args << file_path;

        QProcess process;
        process.setReadChannel(QProcess::StandardOutput);
        process.start(program_decrypt,args);
        process.waitForFinished(-1);

        //QTextStream in(&process->readAll());
        QString out = process.readAll();
        QStringList list_out = out.split("\n",QString::SkipEmptyParts);

        //start resetting model (notify the view )
        beginResetModel();
        this->table.clear();
        std::set<QString> uniq;

        // parse file to model
        // -----------------------
        //while( !in.atEnd() )
        int counter = 0;
        while(counter < list_out.size())
        {
            //QString line = in.readLine();
            QString line = list_out.at(counter);

            // some new UT ?
            // -------------------
            if( !line.isNull() && !line.trimmed().isEmpty() != 0 && !line.contains("="))
            {
                line = line.trimmed();
                std::vector<QString> row;
                if(uniq.find(line) == uniq.end())
                {
                    uniq.insert(line);
                    row.push_back(line);

                    /**QString user_line  = in.readLine().trimmed();
                        QString password_line  = in.readLine().trimmed();
                        QString server_line  = in.readLine().trimmed();*/

                    QString user_line  = list_out.at(++counter).trimmed();
                    QString password_line  = list_out.at(++counter).trimmed();
                    QString server_line  = list_out.at(++counter).trimmed();

                    if( user_line.startsWith("USER") )
                    {
                        user_line = user_line.split('=').at(1).trimmed();
                    }
                    if( password_line.startsWith("PASSWORD") )
                    {
                        password_line = password_line.split('=').at(1).trimmed();
                    }
                    if( server_line.startsWith("SERVER") )
                    {
                        server_line = server_line.split('=').at(1).trimmed();
                    }
                    row.push_back(user_line);
                    row.push_back(password_line);
                    row.push_back(server_line);
                    this->table.push_back(row);
                }
                else
                {
                    QMessageBox msgBox;
                    msgBox.setText(QString("%1 is already defined, ingnoring").arg(line));
                    msgBox.exec();
                }
            }
            ++counter;
        }
        endResetModel();
    }
}

按照@Kuba Ober的建议尝试了解错误的原因

我试图将第一个版本复制到一个主函数中,无限期地在我的所有环境中循环:

但它确实很好用!

#include <QtCore/QCoreApplication>
#include <QProcess>
#include <QTextStream>
#include <set>
#include <vector>
#include <QDebug>
#include <QFile>
#include <deque>
#include <QDir>
#include <QStringList>

std::deque< std::vector < QString > > table;

void f( QString file_path )
{
    // does the param file exist ?
    // --------------------------------
    QFile param_file(file_path);
    if( param_file.exists() && param_file.open(QIODevice::ReadOnly))
    {

        // Call QProcess, fx_decrypt, read standard output
        QString program_decrypt = "/path/to/decrypt";
        QStringList args;
        args << file_path;

        QProcess* process = new QProcess();
        process->setReadChannel(QProcess::StandardOutput);
        process->start(program_decrypt,args);
        process->waitForFinished(-1);

        QTextStream in(&process->readAll());

        //start resetting model (notify the view )
        table.clear();
        std::set<QString> uniq;

        // parse file to model
        // -----------------------
        while( !in.atEnd() )
        {
            QString line = in.readLine();

            // some new UT ?
            // -------------------
            if( !line.isNull() && line.trimmed().isEmpty() != 0 && !line.contains("="))
            {
                line = line.trimmed();
                std::vector<QString> row;
                if(uniq.find(line) == uniq.end())
                {
                    uniq.insert(line);
                    row.push_back(line);

                    QString user_line  = in.readLine().trimmed();
                    QString password_line  = in.readLine().trimmed();
                    QString server_line  = in.readLine().trimmed();
                    if( user_line.startsWith("USER") )
                    {
                        user_line = user_line.split('=').at(1).trimmed();
                    }
                    if( password_line.startsWith("PASSWORD") )
                    {
                        password_line = password_line.split('=').at(1).trimmed();
                    }
                    if( server_line.startsWith("SERVER") )
                    {
                        server_line = server_line.split('=').at(1).trimmed();
                    }
                    row.push_back(user_line);
                    row.push_back(password_line);
                    row.push_back(server_line);
                    table.push_back(row);
                }
                else
                {
                    qDebug() << QString("%1 is already defined, ingnoring").arg(line);
                }
            }
        }
        param_file.close();
        delete process;
    }
}

QStringList getEnvList()
{
    QDir dir("/path/to/env/");
    QStringList list_env;
    foreach(QString folder, dir.entryList(QStringList(),QDir::AllDirs | QDir::NoDotAndDotDot))
    {
        QFile app_file( dir.absolutePath()  + '/' +  folder + '/' + "file.dat" );
        if (app_file.exists())
        {
            list_env.push_back(folder);
        }
    }
    return list_env;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int counter = 0;

    while(true)
    {
        foreach ( QString s, getEnvList() )
        {
            qDebug() << counter++;
            qDebug() << s;
            f(s);
        }
    }

    return a.exec();
}

我不知道,为什么以下两个代码永远不会崩溃,而第一个代码确实......

0 个答案:

没有答案