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

Crash when instantiating QSqlQuery



  • I am puzzled by a strange crash when instantiating QSqlQuery. Here is what I think is the pertinent code:

    --------- in file albumdoa.h ---------
    AlbumDAO&AlbumDAO::instance()
    {
    static AlbumDAO singleton;
    return singleton;
    }

    AlbumDAO(QSqlDatabase& db);
    .
    .
    public:
    std::unique_ptr<std::vector<std::unique_ptr<Album>>> albums() const;
    private:
    QSqlDatabase &m_DB;

    -------- in file albumdoa.cpp ---------
    using namespace std;
    AlbumDAO::AlbumDAO(QSqlDatabase& db) :
    m_BD(db)
    {}

    unique_ptr<vector<unique_ptr<Album>>> AlbumDAO::albums() const
    {
    QSqlQuery query("SELECT * FROM albums", m_DB); // crash
    QSqlQuery query(m_DB); // Tried this: also crash, so it seems to be related to the DB connection

    unique_ptr<vector<unique_ptr<Album>>> list(new vector<unique_ptr<Album>>());
    .
    .
    return list;
    

    }

    The above seems to point to the database connection, but the database is successfylly opened (openStatus is true) in dbinterface.cpp:

    DBinterface::DBinterface(const QString& DBname) :
    m_DB(new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"))),
    m_albumDao(*m_DB)
    {
    m_DB->setHostName("localhost");
    m_DB->setUserName("me");
    m_DB->setDatabaseName(DBname);
    m_DB->setPassword("mysqlpassword");

    bool openStatus = m_DB->open();     // returns true
    .
    .
    

    -- in dbinterfece.h -------
    public:
    const AlbumDAO m_albumDao;

    private:
    std::unique_ptr<QSqlDatabase> m_DB;

    What puzzles me i that the database connection is successfully opened, but the crash seems to indicate that something is wroung with the connection.

    I found several old posts regarding QSqlQuery crashes; here is one:
    https://stackoverflow.com/questions/25660089/qsqlquerymodel-with-a-parent-app-crash,
    but it is 5 years old, so I presume that eventual bugs will have been fixed by now.
    Is there a way to check that the database connection is sound (whatever that means...)? If there is I could do that just before instantiating QSqlQuery.
    By the way, my MySql version is 14.14 Distrib 5.7.30, for Linux (x86_64).



  • @Buller

    Documentation says:

    Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
    

    You are passing the m_db pointer around. This is not recommended. So maybe this causes your problem. QSqlDatabase has static functions to get the current db or add a new connection.



  • I think it is because for some reason the instance m_BD reference to is no longer available.
    BTW, you're not using AlbumDAO::instance, right? That singleton should't be used in that code.
    And as @Pl45m4 said, you don't need to manage the db instance your self.



  • @Bonnie said in Crash when instantiating QSqlQuery:

    I think it is because for some reason the instance m_BD reference to is no longer available.
    BTW, you're not using AlbumDAO::instance, right? That singleton should't be used in that code.
    And as @Pl45m4 said, you don't need to manage the db instance your self.

    I have just found out that the crash was triggered by an out of bound index in another class. I still do not fully understand how the crash did not happen until I instantiated QSqlQuery, but now the code seems to be working just fine.

    I will take another look at how m_BD is used. Thanks for point it out.



  • @Buller
    If you wrote a value into an OOB index elsewhere, almost anything can go wrong/crash/show up, as you may have overwritten an area of memory belonging to another object.


Log in to reply