Results 1 to 11 of 11

Thread: canonicalFilePath() thread problems

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default canonicalFilePath() thread problems

    Using 4.2.1, I ran into some wierd problems running
    Qt Code:
    1. fname2=QFileInfo(fname).canonicalFilePath()
    To copy to clipboard, switch view to plain text mode 
    with multiple threads. The function was occasionally getting mixed up between threads and returning incorrect results.

    So, I mutex'ed it out and everything was fixed. But it was a big deal because I had to use the same mutex on every occurance of QFileInfo throughout my application. I thought that QFileInfo was supposed to be re-entrant ....?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: canonicalFilePath() thread problems

    What happens if you set caching off or call refresh() before canonicalFilePath()? I know this is not a solution but I'm curious if it's just a caching problem.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by jpn View Post
    What happens if you set caching off or call refresh() before canonicalFilePath()? I know this is not a solution but I'm curious if it's just a caching problem.
    Unfortunately (or fortunately, depending on how you look at it)... now that I've made a separate function in my class called "canonical" (for purpose of mutexing the QFileInfo(str).canonicalFilePath()), I can't get it to fail again, --- even without the QMutexLocker So I don't have an answer, but I think I'll keep the mutex locker in there to be safe... it seems like a not completely reproducible potential bug.... the worst kind.

  4. #4
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by jpn View Post
    What happens if you set caching off or call refresh() before canonicalFilePath()? I know this is not a solution but I'm curious if it's just a caching problem.
    Sorry, I spoke too soon on that last post. It IS reproducible, and locking the QMutex IS required. But the refresh() and setCaching(off) does not seem to do the trick, because if I comment out the QMutexLocker line and uncomment the other two lines below, I get the problem.

    Qt Code:
    1. QString canonical(QString str) { //this seems to be necesary due to wierd thread problems
    2. QMutexLocker locker(&canonical_mutex);
    3. QFileInfo info(str);
    4. //info.setCaching(false);
    5. //info.refresh();
    6. return info.canonicalFilePath();
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    Just wanted to follow up on this thread and point out that I am still suffering from thread-related problems with QFileInfo. Functions like canonicalFilePath() simply return the wrong data (rarely, but consistently)

    As a workaround I created a MyQFileInfo class in which I mutex out all the key functions, and it works fine.

    So this is a warning to everyone using QFileInfo with threads.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by magland View Post
    Functions like canonicalFilePath() simply return the wrong data (rarely, but consistently)
    Which Qt version do you use?

  7. #7
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by jacek View Post
    Which Qt version do you use?
    4.3.2 now. In the past I used 4.3.0, same problem.

    Again, problem only arises with multiple threads, and is resolved by locking mutexes.

    J

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by magland View Post
    4.3.2 now. In the past I used 4.3.0, same problem.
    The docs say that QFileInfo is reentrant and they don't say that it isn't thread-safe, so it should work. There were some problems with QFileInfo in the past (see task #103940), so this might be a bug that should be reported to the Trolls.

  9. #9
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    Quote Originally Posted by jacek View Post
    The docs say that QFileInfo is reentrant and they don't say that it isn't thread-safe, so it should work. There were some problems with QFileInfo in the past (see task #103940), so this might be a bug that should be reported to the Trolls.
    Okay, I reported it. Thanks.

  10. #10
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    I was able to reproduce the problem with a single main.cpp (below). I reported to Trolltech, but feel free to try it out on your system. I am using Windows XP.

    Qt Code:
    1. #include <QtGui>
    2. #include <QDir>
    3.  
    4. ///Change these as needed
    5. #define SAMPLE_DIRECTORY_1 "c:\\"
    6. #define SAMPLE_DIRECTORY_2 "c:\\jer"
    7. #define NUM_REPEATS 100
    8. ///When DO_LOCK_MUTEX is turned off, inconsistencies are displayed on my system
    9. //#define DO_LOCK_MUTEX
    10.  
    11. static QMutex my_mutex;
    12.  
    13. class MyThread1 : public QThread
    14. {
    15. public:
    16. void run()
    17. {
    18. QString dirname=SAMPLE_DIRECTORY_1;
    19. QDir dir(dirname);
    20. QStringList list=dir.entryList(QStringList("*"));
    21. for (int j=0; j<NUM_REPEATS; j++) {
    22. foreach (QString F,list) {
    23. #ifdef DO_LOCK_MUTEX
    24. my_mutex.lock();
    25. #endif
    26. QString path=dirname+"/"+F;
    27. QString str1=QFileInfo(path).canonicalFilePath();
    28. QString str2=QFileInfo(path).canonicalFilePath();
    29. if (str1!=str2) {
    30. qDebug() << "~!!!!!!!!!!!!!!!!! Inconsistency with" << str1 << str2;
    31. }
    32. #ifdef DO_LOCK_MUTEX
    33. my_mutex.unlock();
    34. #endif
    35. }
    36. }
    37. }
    38. };
    39.  
    40. class MyThread2 : public QThread
    41. {
    42. public:
    43. void run()
    44. {
    45. QString dirname=SAMPLE_DIRECTORY_2;
    46. QDir dir(dirname);
    47. QStringList list=dir.entryList(QStringList("*"));
    48. for (int j=0; j<NUM_REPEATS; j++) {
    49. foreach (QString F,list) {
    50. #ifdef DO_LOCK_MUTEX
    51. my_mutex.lock();
    52. #endif
    53. QString path=dirname+"/"+F;
    54. QString str1=QFileInfo(path).canonicalFilePath();
    55. QString str2=QFileInfo(path).canonicalFilePath();
    56. if (str1!=str2) {
    57. qDebug() << "~!!!!!!!!!!!!!!!!! Inconsistency with" << str1 << str2;
    58. }
    59. #ifdef DO_LOCK_MUTEX
    60. my_mutex.unlock();
    61. #endif
    62. }
    63. }
    64. }
    65. };
    66.  
    67.  
    68.  
    69.  
    70.  
    71. int main(int argc, char **argv)
    72. {
    73. QApplication app(argc, argv);
    74. QWidget wid;
    75. wid.show();
    76. MyThread1 T1;
    77. MyThread2 T2;
    78. T1.start();
    79. T2.start();
    80.  
    81. qDebug() << "Looking for inconsistencies in QFileInfo::canonicalFilePath()";
    82.  
    83.  
    84. return app.exec();
    85. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: canonicalFilePath() thread problems

    For information, trolls have acknowledged the problem and are now working on it.

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Replies: 10
    Last Post: 20th March 2007, 22:19
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44
  5. Problems with threads and windows
    By SkripT in forum Qt Programming
    Replies: 15
    Last Post: 16th January 2006, 17:46

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.