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

Rohan McGovern rohan.mcgovern at nokia.com
Mon Aug 23 01:15:00 CEST 2010


Jason H said:
> 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?

I can't find the specific cause of the above behavior from the code
you pasted.

But: you really mustn't write a message handler like this.  You've
used several high-level Qt APIs in your message handler.  You don't
know if or when any of those are going to generate messages themselves,
so you're setting yourself up for a stack overflow.  Even if you can get
it to work for your Qt version, you might find the next version adds
some qWarning about deprecated something-or-other and your app suddenly
crashes.

I would consider either:

 - don't put much logic in the handler, just put the message on a queue
   and handle it later, or:

 - rewrite your handler to ignore any messages generated while the
   handler is already running
-- 
Rohan McGovern
QA Engineer
Qt Development Frameworks, Nokia



More information about the Qt-interest-old mailing list