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. why QPixmap along with QScale is causing segmentation fault on qt 5.9 ?
QtWS25 Last Chance

why QPixmap along with QScale is causing segmentation fault on qt 5.9 ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • N Offline
    N Offline
    nikhil_belani
    wrote on last edited by mrjj
    #1

    i have executed below code and it’s giving segmentation fault

    #include <QApplication>
    #include <QLabel>
    #include <QPicture>
    #include <QPainter>
    #include <QPixmap>
    
    int main(int argc, char *argv[])
    {
           QApplication a(argc, argv);
           const QSize qsize (100,100);
           QPixmap *bg = new QPixmap(200,200);
           QPainter* p = new QPainter(bg);
          *bg = bg->scaled(qsize)
           bg->fill(QColor(180, 192, 201));
           p->setPen(QPen(QColor(180, 192, 201),1, Qt::SolidLine));
           p->drawRect(0, 0, 200, 200);
     
           return a.exec();
    }
     (added tags : mrjj)
    

    if we comment *bg = bg->scaled(qsize) ; line no segmentation fault is found .
    issue is found on Qt 5.9 version

    JKSHJ 1 Reply Last reply
    0
    • N nikhil_belani

      i have executed below code and it’s giving segmentation fault

      #include <QApplication>
      #include <QLabel>
      #include <QPicture>
      #include <QPainter>
      #include <QPixmap>
      
      int main(int argc, char *argv[])
      {
             QApplication a(argc, argv);
             const QSize qsize (100,100);
             QPixmap *bg = new QPixmap(200,200);
             QPainter* p = new QPainter(bg);
            *bg = bg->scaled(qsize)
             bg->fill(QColor(180, 192, 201));
             p->setPen(QPen(QColor(180, 192, 201),1, Qt::SolidLine));
             p->drawRect(0, 0, 200, 200);
       
             return a.exec();
      }
       (added tags : mrjj)
      

      if we comment *bg = bg->scaled(qsize) ; line no segmentation fault is found .
      issue is found on Qt 5.9 version

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @nikhil_belani said in why QPixmap along with QScale is causing segmentation fault on qt 5.9 ?:

         QPixmap *bg = new QPixmap(200,200);
         QPainter* p = new QPainter(bg);
      

      Don't use new here. Just allocate QPainter and QPixmap on the stack.

      QPixmap bg1(200, 200);
      QPixmap bg2 = bg1.scaled(qsize);
      

      See http://doc.qt.io/qt-5/implicit-sharing.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      N 1 Reply Last reply
      4
      • JKSHJ JKSH

        @nikhil_belani said in why QPixmap along with QScale is causing segmentation fault on qt 5.9 ?:

           QPixmap *bg = new QPixmap(200,200);
           QPainter* p = new QPainter(bg);
        

        Don't use new here. Just allocate QPainter and QPixmap on the stack.

        QPixmap bg1(200, 200);
        QPixmap bg2 = bg1.scaled(qsize);
        

        See http://doc.qt.io/qt-5/implicit-sharing.html

        N Offline
        N Offline
        nikhil_belani
        wrote on last edited by
        #3

        thanks same was code was working in 4.7 Qt version .
        why its not working with qt 5.9 ??

        JKSHJ 1 Reply Last reply
        0
        • N nikhil_belani

          thanks same was code was working in 4.7 Qt version .
          why its not working with qt 5.9 ??

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @nikhil_belani said in why QPixmap along with QScale is causing segmentation fault on qt 5.9 ?:

          thanks

          You're welcome. Does your updated code work now?

          same was code was working in 4.7 Qt version .
          why its not working with qt 5.9 ??

          I don't know the exact cause, but lots of internal implementations changed between Qt 4.8 and Qt 5.0.

          Even in Qt 4, the recommended way to use QPixmap was by-value, not by-pointer.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

            Hi
            It crashes in the painter that have a reference to the BG and i assume
            its due to implicit sharing being fooled with *b = implicit shared copy of scaled self

            if you flip the order to
            QPixmap *bg = new QPixmap(200,200);
            bg=bg->scaled(qsize);
            QPainter
            p = new QPainter(bg);
            it wont crash.

            However as @JKSH points out. QPixmaps are meant to be used as non pointers.

            1 Reply Last reply
            3

            • Login

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