Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPSQL Multithread Crash



  • Hello,

    Whats wrong with running this code in a thread:

    @ QSqlDatabase db = QSqlDatabase::addDatabase( "QPSQL", QString::number( (long long)(QThread::currentThread())) );
    db.setHostName( QString::fromStdString( m_Hostname ) );
    db.setPort( m_Port );
    db.setDatabaseName(QString::fromStdString( m_DatabaseName ));
    db.setUserName(QString::fromStdString( m_User ));
    db.setPassword(QString::fromStdString( m_PW ));

    bool online = db.open();
    if(!online) {
    //db.close();
    m_HasError = true;
    return DATA_CANCELLED;
    };

    QSqlQuery query(QString::fromStdString( m_CountMeltingsQuery ), db);@

    I mwan this runs, but when i close the MainWindow... The program crashes at:

    qsqlpsqld4.dll!QPSQLDriver::close() Zeile 848 C++ <--- Crash!
    QtSqld4.dll!QSqlDatabase::close() Zeile 882 C++
    ->

    The code in qpsqldriver is:
    @ if (d->connection)
    PQfinish(d->connection);
    d->connection = 0;@



  • Did you read "Threads and the SQL Module":http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-module of the QSqlDatabase documentation?

    Did you open another connection in another thread?
    Are you sure the code is running in the new thread?


Log in to reply