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. QSqlDatabase::addDatabase cause crash???

QSqlDatabase::addDatabase cause crash???

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k 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.
  • N Offline
    N Offline
    nyaruko
    wrote on last edited by
    #1

    I have the following code which is called by many threads using a thread pool:

     @QByteArray PureImageCache::GetImageFromCache(MapType::Types type, Point pos, int zoom)
    
     {   
    
           lock.lockForRead();
    
          QByteArray ar;
    
          if(gtilecache.isEmpty()|gtilecache.isNull())
    
          return ar;
    
    QString dir=gtilecache;
    Mcounter.lock();
    qlonglong id=++ConnCounter;
    Mcounter.unlock();
    
        QString db=dir+"Data.qmdb";
        {
            QSqlDatabase cn;
    
            cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id));
    
            cn.setDatabaseName(db);
            cn.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE");
            if(cn.open())
            {
                QSqlQuery query(cn);
                query.exec(QString("SELECT Tile FROM TilesData WHERE id = (SELECT id FROM Tiles WHERE X=%1 AND Y=%2 AND Zoom=%3 AND Type=%4)").arg(pos.X()).arg(pos.Y()).arg(zoom).arg((int) type));
                query.next();
                if(query.isValid())
                {
                    ar=query.value(0).toByteArray();
                    //qDebug()<<"Got";
                }else{
                    //qDebug()<<"Miss";
                }
                cn.close();
            }
        }
        QSqlDatabase::removeDatabase(QString::number(id));
    lock.unlock();
    return ar;
    

    }@

    However, I have met one crash (not every time) right after I call the:

    cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id));

    function. And the call stack says the error is:

    Debug Assertion Failed!
    File:f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
    Line: 52
    Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

    Is there any thing in my code that is not multithread safe? I am not familiar with the Qt Sql part, can anyone give some suggestions?

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

      Hi and welcome to devnet,

      One thing that is not correct is that you don't release the lock after if(gtilecache.isEmpty()|gtilecache.isNull())

      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
      • N Offline
        N Offline
        nyaruko
        wrote on last edited by
        #3

        Thanks for that. I have asked some of my friends and now there would be some deeper thoughts about this problem, would you like to have a look?
        http://qt-project.org/forums/viewthread/49170/

        [quote author="SGaist" date="1414800164"]Hi and welcome to devnet,

        One thing that is not correct is that you don't release the lock after if(gtilecache.isEmpty()|gtilecache.isNull())[/quote]

        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