Crash in QDateTime::currentDateTime().toString()



  • Hi

    I use QDateTime::currentDateTime() function in multiple threads. In each thread I create new QDateTime object on which I call currentDateTime().toString(). A crash happened and debbuger pointed to globalQDateTime.currentDateTime().toString(). I tried hundreds times but cannot reproduce the crash again. I cannot stop thinking what was the cause of the crash. QDateTime.currentDateTime is a static function and documentation says all functions in the QDateTime class are reentrant. As far as I know it is safe to use static function in secondary threads as long as it is a part of a new object instantiated in those threads. Any help please.

    @//Main Thread:
    QDateTime globalQDateTime;
    globalQDateTime.currentDateTime().toString(); // crash here

    //Thread 1:
    QDateTime tQDateTime; tQDateTime.daysTo(tQSslCertificate.expiryDate());

    //Thread 2:
    QDateTime tQDateTime;
    if (mQDateTime.addSec(3000) > tQDateTime.currentDateTime() ) ...@



  • What signal does it throw? SIGSEGV?



  • [quote author="tucnak" date="1339400575"]What signal does it throw? SIGSEGV?[/quote]

    I didn't saved the debug log. The problem is I cannot replicate it now. I'll do my best to cause the crash again and will post back complete results.



  • Hi, the toString returns a QString so maybe you didn't handle the QString returned properly. That might also cause the segment error?
    The threads may not be the problem.
    greetz



  • Try to use this link.It gives the solution for problem
    http://qt-project.org/doc/qt-4.8/qdatetime.html



  • [quote author="Jeroentje@home" date="1339426002"]Hi, the toString returns a QString so maybe you didn't handle the QString returned properly. That might also cause the segment error?
    The threads may not be the problem.
    greetz[/quote]

    What are you talking about?! Ignoring return value of member function is legal! Absolutely legal in C++ meaning.



  • ENEEDMOREINFO. Could you post a stack trace of the crash? The code looks fine.

    What I am not sure about is whether currentDateTime() is thread safe. If it uses a static object on QDateTime, it probably is not. You would need to serialize access to it then.



  • currentDateTime is thread safe if your system has localtime_r (or is windows)
    localtime is used if localtime_r is not available, and that may not be thread safe (depending on libc implementation)



  • That is really good to know. Is it actually documented somewhere? If not, we can add it to a doc note.



  • QDateTime is documented as being re-entrant.
    It requires the C library to be re-entrant to achieve that though.
    (either by providing the posix thread safe functions or by implementing the standard functions in a thread safe manner)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.