Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crash when instantiating QSqlQuery
Forum Updated to NodeBB v4.3 + New Features

Crash when instantiating QSqlQuery

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 429 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Buller
    wrote on last edited by
    #1

    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).

    Pl45m4P 1 Reply Last reply
    0
    • B Buller

      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).

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        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.

        B 1 Reply Last reply
        1
        • B Bonnie

          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.

          B Offline
          B Offline
          Buller
          wrote on last edited by
          #4

          @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.

          JonBJ 1 Reply Last reply
          0
          • B Buller

            @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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved