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. QPainter drawing crashes the application.
QtWS25 Last Chance

QPainter drawing crashes the application.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 5.9k Views
  • 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.
  • A Offline
    A Offline
    adutzu89
    wrote on last edited by adutzu89
    #1

    I am trying to draw a pixmap which ends up as an icon for QSystemTrayIcon but half of the time I get segmentation faults(sometimes the log says dbus error), I have tried it to keep it simple but to no use I still have the issue.

    I have tried debugging, but if I put a breaking point before the drawing method the application never crashes....yet if I put it after it, I get crashes. The debugger also doesn't say where the crash has occurred.

    Tried making the objects on both heap and stack and the result is the same.
    First version:

            QPixmap pixmap(44,22);
            pixmap.fill(QColor(0,0,0,0));
            QPainter painter(&pixmap);
            painter.setPen(QColor(Qt::white));
            painter.drawText(QRect(0, 0, 22, 22), Qt::AlignCenter, "test");
    

    Second version:

            QPixmap *pixmap = new QPixmap(44,22);
            pixmap->fill(QColor(0,0,0,0));
            QPainter *painter = new QPainter(pixmap);
            painter->setPen(QColor(Qt::white));
            painter->drawText(QRect(0, 0, 22, 22), Qt::AlignCenter, "test");
    

    Tested it in both Unity7 and Xfce with the result being the same. Any ideeas?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Since you are painting on
      a pixmap, i see nothing that can crash it.

      Have you tried simply to return empty pixmap and if it still crashes?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adutzu89
        wrote on last edited by
        #3

        Thanks for the quick answer, returning an empty pixmap does not crash it.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          adutzu89
          wrote on last edited by
          #4

          So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
          I will return after a few days of testing to let you know if it works.

          C A kshegunovK 3 Replies Last reply
          0
          • A adutzu89

            So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
            I will return after a few days of testing to let you know if it works.

            C Offline
            C Offline
            calebhalvy
            wrote on last edited by
            #5

            @adutzu89 glad you found a workaround, but this is not a great solution. There must be something else going on here.

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Can you show the full code where you are preparing the icon ?

              Also, what version of Qt are you using ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • A adutzu89

                So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
                I will return after a few days of testing to let you know if it works.

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                @adutzu89 said in QPainter drawing crashes the application.:

                So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
                I will return after a few days of testing to let you know if it works.

                All your doing here is changing the timing which doesn't actually fix the problem. It might "fix" it on your system but all it needs is a faster (or slower) computer with more (or less) cpus and the crash will reappear.

                This indicates you are having a threading issue, which more than likely is a bad synchronized object. Check your mutexes and places you share memory and you should find your crash.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                1
                • A adutzu89

                  So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
                  I will return after a few days of testing to let you know if it works.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @adutzu89 said in QPainter drawing crashes the application.:

                  I have put the thread to sleep for 500 msecs

                  What thread might that be? QPixmap isn't reentrant to begin with!

                  The debugger also doesn't say where the crash has occurred.

                  Of course it does, it wouldn't be much of a debugger otherwise. When the crash occurs pull out the stack trace and paste it here, please.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • A Offline
                    A Offline
                    adutzu89
                    wrote on last edited by
                    #9

                    @kshegunov said in QPainter drawing crashes the application.:

                    @adutzu89 said in QPainter drawing crashes the application.:

                    I have put the thread to sleep for 500 msecs

                    What thread might that be? QPixmap isn't reentrant to begin with!

                    The debugger also doesn't say where the crash has occurred.

                    Of course it does, it wouldn't be much of a debugger otherwise. When the crash occurs pull out the stack trace and paste it here, please.

                    The thread in which the method was called, I've used the static call to QThread::msleep(500), the strack was empty as far as I remember and if what I think is the cause isn't then I will remake the conditions and make a screenshot.

                    @ambershark said in QPainter drawing crashes the application.:

                    @adutzu89 said in QPainter drawing crashes the application.:

                    So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
                    I will return after a few days of testing to let you know if it works.

                    All your doing here is changing the timing which doesn't actually fix the problem. It might "fix" it on your system but all it needs is a faster (or slower) computer with more (or less) cpus and the crash will reappear.

                    This indicates you are having a threading issue, which more than likely is a bad synchronized object. Check your mutexes and places you share memory and you should find your crash.

                    This was happening in the main thread, here is how it gets to the method containing the drawing:

                    • make a QObject-based class
                    • register it as a QML type
                    • change property value
                    • call method

                    After reading all your comments I'm thinking that the cause might be the object setting 2 values to one property at the sametime. That property set method calls the method with the drawing code.

                    // here I change the value of the property, call the drawing method and emit the change:
                    void TrayController::setIcon(const QString &icon) {
                        if (m_icon != icon) {
                            setTrayIcon(icon);
                            emit iconChanged();
                        }
                    }
                    
                    // here I draw the text on the pixmap
                    void TrayController::setTrayIcon(const QString &weather) {
                        QPixmap pixmap(22,22);
                        pixmap.fill(QColor(0,0,0,0));
                        QPainter painter(&pixmap);
                        painter.setPen(QColor(Qt::white));
                        painter.drawText(QRect(0, 0, 22, 22), Qt::AlignCenter, weather);
                        trayIcon->setIcon(QIcon(pixmap));
                    }
                    

                    I will try adding a mutex locker and will try ensuring that it will the method if no value was set.

                    @SGaist said in QPainter drawing crashes the application.:

                    Hi,

                    Can you show the full code where you are preparing the icon ?

                    Also, what version of Qt are you using ?

                    Using Qt 5.6.2, 64 bit version.

                    kshegunovK 1 Reply Last reply
                    0
                    • A adutzu89

                      @kshegunov said in QPainter drawing crashes the application.:

                      @adutzu89 said in QPainter drawing crashes the application.:

                      I have put the thread to sleep for 500 msecs

                      What thread might that be? QPixmap isn't reentrant to begin with!

                      The debugger also doesn't say where the crash has occurred.

                      Of course it does, it wouldn't be much of a debugger otherwise. When the crash occurs pull out the stack trace and paste it here, please.

                      The thread in which the method was called, I've used the static call to QThread::msleep(500), the strack was empty as far as I remember and if what I think is the cause isn't then I will remake the conditions and make a screenshot.

                      @ambershark said in QPainter drawing crashes the application.:

                      @adutzu89 said in QPainter drawing crashes the application.:

                      So I have tried something, I have put the thread to sleep for 500 msecs and it might solved it after aprox 10-15 application startups it did not crashed.
                      I will return after a few days of testing to let you know if it works.

                      All your doing here is changing the timing which doesn't actually fix the problem. It might "fix" it on your system but all it needs is a faster (or slower) computer with more (or less) cpus and the crash will reappear.

                      This indicates you are having a threading issue, which more than likely is a bad synchronized object. Check your mutexes and places you share memory and you should find your crash.

                      This was happening in the main thread, here is how it gets to the method containing the drawing:

                      • make a QObject-based class
                      • register it as a QML type
                      • change property value
                      • call method

                      After reading all your comments I'm thinking that the cause might be the object setting 2 values to one property at the sametime. That property set method calls the method with the drawing code.

                      // here I change the value of the property, call the drawing method and emit the change:
                      void TrayController::setIcon(const QString &icon) {
                          if (m_icon != icon) {
                              setTrayIcon(icon);
                              emit iconChanged();
                          }
                      }
                      
                      // here I draw the text on the pixmap
                      void TrayController::setTrayIcon(const QString &weather) {
                          QPixmap pixmap(22,22);
                          pixmap.fill(QColor(0,0,0,0));
                          QPainter painter(&pixmap);
                          painter.setPen(QColor(Qt::white));
                          painter.drawText(QRect(0, 0, 22, 22), Qt::AlignCenter, weather);
                          trayIcon->setIcon(QIcon(pixmap));
                      }
                      

                      I will try adding a mutex locker and will try ensuring that it will the method if no value was set.

                      @SGaist said in QPainter drawing crashes the application.:

                      Hi,

                      Can you show the full code where you are preparing the icon ?

                      Also, what version of Qt are you using ?

                      Using Qt 5.6.2, 64 bit version.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @adutzu89 said in QPainter drawing crashes the application.:

                      The thread in which the method was called

                      The question is rather is this the main thread, if it's not then you're doing it wrong. I repeat, again, QPixmap is not reentrant. If you don't know what's the implication of that, then it basically means you can't use QPixmap objects in different threads!

                      Read and abide by the Qt Code of Conduct

                      A 1 Reply Last reply
                      2
                      • kshegunovK kshegunov

                        @adutzu89 said in QPainter drawing crashes the application.:

                        The thread in which the method was called

                        The question is rather is this the main thread, if it's not then you're doing it wrong. I repeat, again, QPixmap is not reentrant. If you don't know what's the implication of that, then it basically means you can't use QPixmap objects in different threads!

                        A Offline
                        A Offline
                        adutzu89
                        wrote on last edited by
                        #11

                        @kshegunov said in QPainter drawing crashes the application.:

                        @adutzu89 said in QPainter drawing crashes the application.:

                        The thread in which the method was called

                        The question is rather is this the main thread, if it's not then you're doing it wrong. I repeat, again, QPixmap is not reentrant. If you don't know what's the implication of that, then it basically means you can't use QPixmap objects in different threads!

                        You mean it's not thread safe, right?
                        Anyway, I know this reads odd but yesterday evening not a single crashed occured while testing. The thread is the same as the main GUI thread from my knowledge, tough I forgot to test it yesterday to ensure it. I think I will also give QImage a try since it seems it's more recommended than QPixmap and afterwards will turn it to QPixmap when setting the icon.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          For image processing in secondary threads, yes QImage is the way to go. The class is made for that. QPixmap on the other hand is optimised for showing images on screen.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

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