Results 1 to 7 of 7

Thread: Could this be a possible bug for Qt’s SQL database?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2014
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Could this be a possible bug for Qt’s SQL database?

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

    Qt Code:
    1. QByteArray PureImageCache::GetImageFromCache(MapType::Types type, Point pos, int zoom)
    2.  
    3. {
    4.  
    5. lock.lockForRead();
    6.  
    7.  
    8. if(gtilecache.isEmpty()|gtilecache.isNull())
    9.  
    10. return ar;
    11.  
    12. QString dir=gtilecache;
    13. Mcounter.lock();
    14. qlonglong id=++ConnCounter;
    15. Mcounter.unlock();
    16.  
    17. QString db=dir+"Data.qmdb";
    18. {
    19.  
    20. cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id));
    21.  
    22. cn.setDatabaseName(db);
    23. cn.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE");
    24. if(cn.open())
    25. {
    26. QSqlQuery query(cn);
    27. 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));
    28. query.next();
    29. if(query.isValid())
    30. {
    31. ar=query.value(0).toByteArray();
    32. //qDebug()<<"Got";
    33. }else{
    34. //qDebug()<<"Miss";
    35. }
    36. cn.close();
    37. }
    38. }
    39. QSqlDatabase::removeDatabase(QString::number(id));
    40. lock.unlock();
    41. return ar;
    42. }
    To copy to clipboard, switch view to plain text mode 
    However, I have met one crash (not every time) right after I call the:

    Qt Code:
    1. cn = QSqlDatabase::addDatabase(“QSQLITE”,QString::number(id));
    To copy to clipboard, switch view to plain text mode 

    function. And the call stack says the error is:

    Qt Code:
    1. Debug Assertion Failed!
    2. File:f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
    3. Line: 52
    4. Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    To copy to clipboard, switch view to plain text mode 

    The cause is within the = operator, it call an atomic assign where inside that atomic assign, there's a delete function cause the crash.
    I noticed that there's a shared_null pointer inside the default constructor of QSqlDatabase.
    Here is the thing:
    The default constructor of QSqlDatabase used a shared_null.
    Note that the shared_null is an object indicating an invalid database, not a plain null pointer.

    Here is the code: http://code.woboq.org/qt5/qtbase/src...1shared_nullEv
    Could it be that the delete is called on to delete the shared_null which is essentially some static variable and caused the crash?
    How could I avoid this and continue using QSqlDataBase safely in multi-thread senario?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Could this be a possible bug for Qt’s SQL database?

    What happens when you merge lines 20 and 22 so that it looks like every example in the docs and you never default construct an invalid QSqlDatabase?
    Qt Code:
    1. QSqlDatabase cn = QSqlDatbase::addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2014
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: Could this be a possible bug for Qt’s SQL database?

    Yes, I did merge it. But there's still crash. And the crash still comes out from the = operator.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Could this be a possible bug for Qt’s SQL database?

    How about putting a muted around calls that add or remove databases?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Could this be a possible bug for Qt’s SQL database?

    Off hand, it looks like there is a test to not attempt a deletion in the destructor of the


    Qt Code:
    1. 181 QSqlDatabasePrivate::~QSqlDatabasePrivate()
    2. 182 {
    3. 183 if (driver != shared_null()->driver)
    4. 184 delete driver;
    5. 185 }
    To copy to clipboard, switch view to plain text mode 

    the assigment operator is as follows:

    Qt Code:
    1. 710 QSqlDatabase &QSqlDatabase::operator=(const QSqlDatabase &other)
    2. 711 {
    3. 712 qAtomicAssign(d, other.d);
    4. 713 return *this;
    5. 714 }
    To copy to clipboard, switch view to plain text mode 

    The type d is on the private database type, so, even though qAtomicAssign will indeed delete the other.d, that destructor will not cause the shared NULL to be deleted. The value for d is then immediately assigned a new value.

    Qt Code:
    1. template <typename T>
    2. 232 inline void qAtomicAssign(T *&d, T *x)
    3. 233 {
    4. 234 if (d == x)
    5. 235 return;
    6. 236 x->ref.ref();
    7. 237 if (!d->ref.deref())
    8. 238 delete d;
    9. 239 d = x;
    10. 240 }
    To copy to clipboard, switch view to plain text mode 

    I am not saying that you are wrong, but, I am saying that

    1. The code looks OK from my quick peek.
    2. I don't see any problems in your code either


  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Could this be a possible bug for Qt’s SQL database?

    This is a code of a single object but Qt has to store a list of registered databases somewhere. I think you might just add the mutex and see if it changes anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Could this be a possible bug for Qt’s SQL database?

    Btw, might I suggest using QReadLocker for your main lock?
    Currently you have at least one return without a matching unlock()

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 6th April 2014, 11:07
  2. QSQLITE database changes not showing up in database
    By Cyrebo in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2013, 23:18
  3. Replies: 2
    Last Post: 27th August 2012, 03:27
  4. [SOLVED] database opened .. database not open
    By kapitanluffy in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:39
  5. Replies: 9
    Last Post: 20th May 2010, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.