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 in qsqlite module
QtWS25 Last Chance

Crash in qsqlite module

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 720 Views
  • 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.
  • E Offline
    E Offline
    euchkatzl
    wrote on last edited by
    #1

    Hello,
    maybe somebody can help me or give me some hints what i can do to avoid such crashes ?

    Bildschirmfoto 2020-09-08 um 08.19.40.png

    What im doing:

    • Each Image is stored via an unique hash and the image data in my sqlite database
    • When trying to display the image (via GraphicItem) the data is loaded from the sqlite database with the image hash
    • This all happens Multithreaded. So for each Loading attempt an own sql connection is made because QSqlDatabase can only be accessed in the thread being created
    • The crash is not reproducible on my machine

    I am using Qt Version 5.12.9.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How are you setting up the database connection ?

      Might be unrelated but how are you getting the image back to your QGraphicsItem ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        euchkatzl
        wrote on last edited by
        #3
        bool DbFile::requestDb(QSqlDatabase &db) const {
        
            // mkh20130206: QSqlDatabase can only be accessed in the thread being created
            int tid = _threadId();
            QString connectionName = QString("%1-%2").arg(mFileName).arg(tid);
        
            bool retVal = false;
            if (QSqlDatabase::contains(connectionName)) {
                db = QSqlDatabase::database(connectionName);
                retVal = db.isValid() && db.isOpen();
            } else {
                db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
                if (db.isValid()) {
                    db.setDatabaseName(mFileName);
                    retVal = db.open();
                    if (retVal) {
                        // store created db connection for later destroy
                        QMutexLocker _accessLock(&mConnectionsLock);
                        mConnections << connectionName;
                    }
                }
            }
            return retVal;
        }
        

        That's the code for the creation of the database connection.

        The Image is sent back as QByteArray. After that is is used for creating QPixmap or QImage (depends on the use case):

        data = q.value(0).toByteArray();
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Is that requestDb method called in a method that runs in the thread using the connection ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            euchkatzl
            wrote on last edited by
            #5

            Yes. The requestDb Method ensures that a connection is used which is created only for that Thread.
            Therefore a unique thread id is generated and used as connection name. Look at the following line.

            int tid = _threadId();
            QString connectionName = QString("%1-%2").arg(mFileName).arg(tid);
            
            static QHash<QThread*, int> _threadIds;
            static int _threadCounter;
            static QMutex _threadIdLock(QMutex::Recursive);
            
            int _threadId() {
                QMutexLocker _accessLock(&_threadIdLock);
                QThread *curThread = QThread::currentThread();
                if (!_threadIds.contains(curThread)) {
                    if (qApp->thread() == curThread) {
                        _threadIds[curThread] = 0;
                    } else {
                        _threadIds[curThread] = ++_threadCounter;
                    }
                }
                return _threadIds[curThread];
            }
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              SQlite is not threadsafe by default: https://sqlite.org/threadsafe.html
              Also Qt does not compile the sqlite plugin with SQLITE_THREADSAFE

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • E Offline
                E Offline
                euchkatzl
                wrote on last edited by
                #7

                Is there any possibillity to get this working for multi threaded applications ?

                Christian EhrlicherC 1 Reply Last reply
                0
                • E euchkatzl

                  Is there any possibillity to get this working for multi threaded applications ?

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @euchkatzl Compile the qt sql plugin with the define I mentioned and activate it as described in the link I gave you.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • E Offline
                    E Offline
                    euchkatzl
                    wrote on last edited by
                    #9

                    Thank you. I will try this

                    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