QPainter drawing crashes the application.



  • 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?


  • Qt Champions 2016

    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?



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



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



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


  • Lifetime Qt Champion

    Hi,

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

    Also, what version of Qt are you using ?



  • @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.


  • Qt Champions 2016

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



  • @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.


  • Qt Champions 2016

    @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!



  • @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.


  • Lifetime Qt Champion

    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.


Log in to reply
 

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