Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crash in QDateTime::currentDateTime().toString()
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 9.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marcus.fr
    wrote on last edited by
    #1

    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() ) ...@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      What signal does it throw? SIGSEGV?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcus.fr
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          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

          Greetz, Jeroen

          1 Reply Last reply
          0
          • S Offline
            S Offline
            soundar
            wrote on last edited by
            #5

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

            Don't Follow Anyone but You Must Learn From Everyone.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tucnak
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                miroslav
                wrote on last edited by
                #7

                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.

                Mirko Boehm | mirko@kde.org | KDE e.V.
                FSFE Fellow
                Qt Certified Specialist

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  shanek
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    miroslav
                    wrote on last edited by
                    #9

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

                    Mirko Boehm | mirko@kde.org | KDE e.V.
                    FSFE Fellow
                    Qt Certified Specialist

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      shanek
                      wrote on last edited by
                      #10

                      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)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved