QSqlDatabase::addDatabase cause crash???



  • 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?


  • Lifetime Qt Champion

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



  • 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]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.