PDA

View Full Version : QFile, QIODevice bug during appending text?



jackmack
19th June 2012, 09:53
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:


void Trace::write(const QString text)
{
if (!m_enabled)
return;

QString filename = getFilenameToday();

if (filename.isEmpty())
return;

QString str;
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");

if (!QFile::exists(filename))
{
str = QString("************************** Startup %1 **************************\n")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}

str += "\n" + timestamp + ": " + text;
str.replace("\r\n", "\n");


m_file.setFileName(filename);

if (m_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered))
{
m_file.write(str.toLatin1());
m_file.flush();
m_file.close();
}

deleteOldFiles();
}

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...

wysota
19th June 2012, 11:25
Are you using threads?

jackmack
19th June 2012, 16:24
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

amleto
19th June 2012, 16:45
"No. The one and only thing I use one QThread is..."
euhm, that should be a 'yes (but it's disabled at the moment)' :p

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.

jackmack
19th June 2012, 17:36
"No. The one and only thing I use one QThread is..."
euhm, that should be a 'yes (but it's disabled at the moment)' :p

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.

Lesiok
19th June 2012, 17:58
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.

jackmack
20th June 2012, 08:54
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!