[Qt-interest] Problems with files and mutexes and deadlocks

Jason H scorp1us at yahoo.com
Fri Aug 20 21:07:27 CEST 2010


I register a debug msg handler.

In that handler,  itbasically does: 
{ 
logFileMutex.lock() ;
( open QFile and write the message, close the file ) 
logFileMutex.unlock() ;
printf (message);
}

When the function completes it calls the following destructors:in the following 
order
QFile()::~ 
QIODevice::~ 
QObject::~ 

But in the QObject destructor, it creates a QMutexLocker, and in its constructor 
calls relock() that calls lock() and then that fires off a warning 
("QMutex::lock: Deadlock detected in thread %ld"). This warning does to the 
registered handler that repeats the same sequence until I get a stack overflow.

How can I avoid this. Why is the deadlock even happening?

Thanks!
----- code follows -----
void myMessageOutput(QtMsgType type, const char *msg)
{
    if (strcmp("QMutex::lock: Deadlock detected in thread 852", msg)==0) return;
    char* level;

    QFile f(logFilePath);
    if (f.size() > 1024*1024 )
    {
        QFile::remove(logFilePath+".sav");
        QFile::rename(logFilePath, logFilePath+".sav");
    }

    switch (type) {
     case QtDebugMsg: level = "Debug:"; break;
     case QtWarningMsg: level = "Warning:"; break;
     case QtCriticalMsg: level = "Critical:"; break;
     case QtFatalMsg: level = "Fatal:"; break;
    }
    logFileMutex.lock();
    if (f.open(QIODevice::Append))
    {
        f.write(QDateTime::currentDateTime().toString(Qt::ISODate).toUtf8());
        f.write(level, strlen(level));
        f.write(msg);
        f.write("\r\n");
        f.close();
    }
    logFileMutex.unlock();
    printf("%s %s %s\r\n", 
QDateTime::currentDateTime().toString(Qt::ISODate).toAscii().data(), level, 
msg);
    if (type==QtFatalMsg)
        abort();
}



      



More information about the Qt-interest-old mailing list