Results 1 to 7 of 7

Thread: QFile, QIODevice bug during appending text?

  1. #1
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QFile, QIODevice bug during appending text?

    Hi all,

    I use Qt 4.8.2 on WindowsXP, VS2010.

    It seems I found a bug in QFile during appending text. Long time ago I wrote a simple "Trace" class for writing data (text, xml) based on Qt 4.7.8. Never a problem occured.

    But now with Qt 4.8.2 the content of the file is confused. My "Trace" class normally writes line by line with a timestamp at beginning and appends a new line at the end. But sometimes whole lines missing or older lines at beginning of the file are overwritten.

    A simple example is to write a "socket connection state" into the file. Inside the slots of connected/disconnected/errorOccured I write a line to the trace.

    Here is a code snipped of my Trace::write() method, the member m_file is a QFile:
    Qt Code:
    1. void Trace::write(const QString text)
    2. {
    3. if (!m_enabled)
    4. return;
    5.  
    6. QString filename = getFilenameToday();
    7.  
    8. if (filename.isEmpty())
    9. return;
    10.  
    11. QString str;
    12. QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
    13.  
    14. if (!QFile::exists(filename))
    15. {
    16. str = QString("************************** Startup %1 **************************\n")
    17. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    18. }
    19.  
    20. str += "\n" + timestamp + ": " + text;
    21. str.replace("\r\n", "\n");
    22.  
    23.  
    24. m_file.setFileName(filename);
    25.  
    26. if (m_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered))
    27. {
    28. m_file.write(str.toLatin1());
    29. m_file.flush();
    30. m_file.close();
    31. }
    32.  
    33. deleteOldFiles();
    34. }
    To copy to clipboard, switch view to plain text mode 

    I now often opening and closing a file is time consuming but this allows to me to open/copy/delete the file during my application is running.

    Have anybody an idea what's going wrong? I looked into the changes of 4.8.1 and 4.8.2 and there were fixes of QFile and QIODevice. Maybe they fixed one bug and added a new one ;-)

    Thanks a lot...

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

    Default Re: QFile, QIODevice bug during appending text?

    Are you using threads?
    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.


  3. #3
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile, QIODevice bug during appending text?

    No. The one and only thing I use one QThread is in my database class (open, read, write into database is done by a workerthread).
    But thread's start() I have comment out currently. Also the communication between the workerthread and the MainWindow is completly solved with signals/slots

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile, QIODevice bug during appending text?

    "No. The one and only thing I use one QThread is..."
    euhm, that should be a 'yes (but it's disabled at the moment)'

    Just because you do things with signal/slot doesn't mean that you are protected against threading issues when writing to text file.

    From the symptoms, it feels like there must be multiple things opening the file. You also say 'sometimes whole line missing...' which implies you can't consistently repeat the issue. This also hints at multi threaded cause.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile, QIODevice bug during appending text?

    Quote Originally Posted by amleto View Post
    "No. The one and only thing I use one QThread is..."
    euhm, that should be a 'yes (but it's disabled at the moment)'

    Just because you do things with signal/slot doesn't mean that you are protected against threading issues when writing to text file.

    From the symptoms, it feels like there must be multiple things opening the file. You also say 'sometimes whole line missing...' which implies you can't consistently repeat the issue. This also hints at multi threaded cause.
    Yes, that's the same I think. But I'm very careful with using thread and I use only if I have no chance to avoid it.

    It seems I have fixed it! Using the member "QFile m_file" causes issues. But when I use only inside the write() method a locally generated QFile instance (on stack!) then it works! But I will test it more exactly.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile, QIODevice bug during appending text?

    Quote Originally Posted by jackmack View Post
    No. The one and only thing I use one QThread is in my database class (open, read, write into database is done by a workerthread).
    But thread's start() I have comment out currently. Also the communication between the workerthread and the MainWindow is completly solved with signals/slots
    Does this thread use Trace class ? If yes use QMutex to synchronise all users of Trace class.

  7. #7
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile, QIODevice bug during appending text?

    No, the thread doesn't use the trace class.

    My trace widget has the method slotAddTrace(const QString). Inside this slot the text is 1) added to a QTextEdit and 2) written to the QFile.
    ThenI have a MySocket:QTcpSocket and a MyDatabase:QObject which contains a workerthread. The signals of MySocket::signalTrace(const QString) and MyDatabase::signalTrace(const QString) are connected to TraceWidget::slotAddTrace(const QString).

    By the way: I have disabled the MyDatabase class. Only one socket is connected to MyTrace. That's strange!

Similar Threads

  1. Replies: 4
    Last Post: 17th May 2009, 18:21
  2. Appending text to a file
    By janus in forum General Programming
    Replies: 8
    Last Post: 25th March 2009, 11:23
  3. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 09:09
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. Replies: 2
    Last Post: 17th November 2006, 11:25

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.