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. Setting QPixmap to Qlabel gives segmentation fault and app crashes

Setting QPixmap to Qlabel gives segmentation fault and app crashes

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5qlabelqpixmapqimagegstreamer1.0
22 Posts 5 Posters 4.7k 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.
  • V Offline
    V Offline
    vicky_mac
    wrote on last edited by
    #1
    GstFlowReturn MainWindow::newSample1(GstAppSink *sink1, gpointer gSelf)
    {
        GstSample* sample = NULL;
        GstBuffer* sampleBuffer = NULL;
        GstMapInfo bufferInfo;
        MainWindow* self = static_cast<MainWindow* >(gSelf);
        sample = gst_app_sink_pull_sample(GST_APP_SINK(sink1));
        QImage image ;
        QPixmap image1 ;
        if(sample != NULL)
        {
            sampleBuffer = gst_sample_get_buffer(sample);
            if(sampleBuffer != NULL)
            {
                gst_buffer_map(sampleBuffer, &bufferInfo, GST_MAP_READ);
                image = QImage(bufferInfo.data, 640, 480, QImage::Format_Grayscale8);
                image1 = QPixmap::fromImage(image);
               
                 if(!image1.isNull())
                 { 
    self->ui->CAM_VDO->setPixmap(image1);       
                 }           
                gst_buffer_unmap(sampleBuffer, &bufferInfo);
    
            }
    
            gst_sample_unref(sample);
        }
    
        return GST_FLOW_OK;
    }
    

    This application crashes and gives segmentation fault error.
    But when i remove or comment this line :
    self->ui->CAM_VDO->setPixmap(image1);
    it runs fine.

    Can anyone please let me know why it's happening.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • V vicky_mac
      GstFlowReturn MainWindow::newSample1(GstAppSink *sink1, gpointer gSelf)
      {
          GstSample* sample = NULL;
          GstBuffer* sampleBuffer = NULL;
          GstMapInfo bufferInfo;
          MainWindow* self = static_cast<MainWindow* >(gSelf);
          sample = gst_app_sink_pull_sample(GST_APP_SINK(sink1));
          QImage image ;
          QPixmap image1 ;
          if(sample != NULL)
          {
              sampleBuffer = gst_sample_get_buffer(sample);
              if(sampleBuffer != NULL)
              {
                  gst_buffer_map(sampleBuffer, &bufferInfo, GST_MAP_READ);
                  image = QImage(bufferInfo.data, 640, 480, QImage::Format_Grayscale8);
                  image1 = QPixmap::fromImage(image);
                 
                   if(!image1.isNull())
                   { 
      self->ui->CAM_VDO->setPixmap(image1);       
                   }           
                  gst_buffer_unmap(sampleBuffer, &bufferInfo);
      
              }
      
              gst_sample_unref(sample);
          }
      
          return GST_FLOW_OK;
      }
      

      This application crashes and gives segmentation fault error.
      But when i remove or comment this line :
      self->ui->CAM_VDO->setPixmap(image1);
      it runs fine.

      Can anyone please let me know why it's happening.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

      self->ui->CAM_VDO

      is CAM_VDO a valid pointer?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      1
      • V vicky_mac
        GstFlowReturn MainWindow::newSample1(GstAppSink *sink1, gpointer gSelf)
        {
            GstSample* sample = NULL;
            GstBuffer* sampleBuffer = NULL;
            GstMapInfo bufferInfo;
            MainWindow* self = static_cast<MainWindow* >(gSelf);
            sample = gst_app_sink_pull_sample(GST_APP_SINK(sink1));
            QImage image ;
            QPixmap image1 ;
            if(sample != NULL)
            {
                sampleBuffer = gst_sample_get_buffer(sample);
                if(sampleBuffer != NULL)
                {
                    gst_buffer_map(sampleBuffer, &bufferInfo, GST_MAP_READ);
                    image = QImage(bufferInfo.data, 640, 480, QImage::Format_Grayscale8);
                    image1 = QPixmap::fromImage(image);
                   
                     if(!image1.isNull())
                     { 
        self->ui->CAM_VDO->setPixmap(image1);       
                     }           
                    gst_buffer_unmap(sampleBuffer, &bufferInfo);
        
                }
        
                gst_sample_unref(sample);
            }
        
            return GST_FLOW_OK;
        }
        

        This application crashes and gives segmentation fault error.
        But when i remove or comment this line :
        self->ui->CAM_VDO->setPixmap(image1);
        it runs fine.

        Can anyone please let me know why it's happening.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @vicky_mac

        WTH is self->ui-> ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        V 1 Reply Last reply
        1
        • jsulmJ jsulm

          @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

          self->ui->CAM_VDO

          is CAM_VDO a valid pointer?

          V Offline
          V Offline
          vicky_mac
          wrote on last edited by
          #4

          @jsulm yes it runs for 1 or 2 sec initially some time more too.
          but then crashes... Also shows images on CAM_VDO label in UI

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @vicky_mac

            WTH is self->ui-> ?

            V Offline
            V Offline
            vicky_mac
            wrote on last edited by
            #5

            @J-Hilk said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

            elf->ui->

            it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

            J.HilkJ 1 Reply Last reply
            0
            • V vicky_mac

              @J-Hilk said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

              elf->ui->

              it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

              it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

              I smell a very very big potential error source here.

              Children should not know of object instanced of their parents! One is supposed to use Signal & Slots.

              Please show how you designed this


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              V 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

                it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

                I smell a very very big potential error source here.

                Children should not know of object instanced of their parents! One is supposed to use Signal & Slots.

                Please show how you designed this

                V Offline
                V Offline
                vicky_mac
                wrote on last edited by
                #7

                @J-Hilk i am using gstreamer for decoding a video ... and appsink to capture the data and display it into qt label..

                this the signal emitted by gstreamer when new sample is ready.

                g_signal_connect(appsink,"new-sample",G_CALLBACK(newSample1),(gpointer)this);

                and it calls above given function.

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

                  Hi,

                  Beside what was already said, you might also be hitting a life time issue. Unless bufferInfo.data is const you are triggering this QImage constructor which requires the buffer to be valid as long as the QImage built with it.

                  In any case, you should not update GUI elements directly from a callback as it might be running in a different thread. You can use QMetaObject::invokeMethod with a queued connection to call a slot that will do the QLabel work. Just create the proper QImage in your callback.

                  Note that with recent version of Qt, you can use custom pipeline with QMediaPlayer which might be way simpler to use.

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

                  V 2 Replies Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    Beside what was already said, you might also be hitting a life time issue. Unless bufferInfo.data is const you are triggering this QImage constructor which requires the buffer to be valid as long as the QImage built with it.

                    In any case, you should not update GUI elements directly from a callback as it might be running in a different thread. You can use QMetaObject::invokeMethod with a queued connection to call a slot that will do the QLabel work. Just create the proper QImage in your callback.

                    Note that with recent version of Qt, you can use custom pipeline with QMediaPlayer which might be way simpler to use.

                    V Offline
                    V Offline
                    vicky_mac
                    wrote on last edited by
                    #9

                    @SGaist thank for suggestion ...

                    AS i am running qt version 5.11.3 and due to some resctrictions i cannot update it.

                    So i think QMetaObject::invokeMethod approch i'll have to follow . But i don't know how to use it.
                    Do you have any example code or any link where i can get some more details on this.

                    *i tried copying the bufferInfo.data into another buffer and then did the image building and display. Then it works .. Don't know wther i should do this or not.

                    As suggested above i must not update UI from call back function . I'll try to use QMetaObject::invokeMethod and come back to you.

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      Beside what was already said, you might also be hitting a life time issue. Unless bufferInfo.data is const you are triggering this QImage constructor which requires the buffer to be valid as long as the QImage built with it.

                      In any case, you should not update GUI elements directly from a callback as it might be running in a different thread. You can use QMetaObject::invokeMethod with a queued connection to call a slot that will do the QLabel work. Just create the proper QImage in your callback.

                      Note that with recent version of Qt, you can use custom pipeline with QMediaPlayer which might be way simpler to use.

                      V Offline
                      V Offline
                      vicky_mac
                      wrote on last edited by
                      #10

                      @SGaist can you please suggest how i can use QMetaObject::invokeMethod in this scenario. I am not able to figure it out.

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

                        Do you have a slot on your widget to set the QPixmap on the label ?

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

                        V 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Do you have a slot on your widget to set the QPixmap on the label ?

                          V Offline
                          V Offline
                          vicky_mac
                          wrote on last edited by
                          #12

                          @SGaist No i was directly updating as given above in the coed.

                          I am not able to figure out on which event i should write the slot for label updation.

                          please suggest.

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

                            Which code exactly ?

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

                            V 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Which code exactly ?

                              V Offline
                              V Offline
                              vicky_mac
                              wrote on last edited by
                              #14

                              @SGaist
                              In the question it self I have given code where I am updating the label.

                              As I receive a frame in gst reamer thread I update it.

                              But how to do it in signal slot mechanism I am not able to feagure out. As you suggested above using qmetaobject how to use it in code.?

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

                                As I receive a frame in gst reamer thread I update it.

                                You are not allowed to modify the GUI from any thread except the main thread (better: the thread where QApplication lives in). Use signals/slots to update your ui.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

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

                                  Something like:

                                  QMetaObject::invokeMethod(ui->label, "setPixmap", Qt::QueuedConnection, Q_ARG(pixmap));
                                  

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

                                  V 1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    Something like:

                                    QMetaObject::invokeMethod(ui->label, "setPixmap", Qt::QueuedConnection, Q_ARG(pixmap));
                                    
                                    V Offline
                                    V Offline
                                    vicky_mac
                                    wrote on last edited by
                                    #17

                                    @SGaist hi thanx for advise..
                                    I did Qmetaobject::Invokemethod(self-ui->vdolabel, "setPixel", qtqueuedconnection, Q_Args(Qpixmap, P)) :

                                    And updating qlabel from setPixmap function in mainwidown.cpp its working fine.

                                    Is it ok now or should have to anything else.
                                    Is this OK passing the object reference and derefenrecing it like this?
                                    MainWindow* self = static_cast<MainWindow* >(gSelf);

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

                                      Do you mean pass your MainWindow instance as the opaque data to your callback ?

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

                                      V 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        Do you mean pass your MainWindow instance as the opaque data to your callback ?

                                        V Offline
                                        V Offline
                                        vicky_mac
                                        wrote on last edited by
                                        #19

                                        @SGaist yes, because without that we would not Abe able to invoke method.
                                        MainWindow* self = static_cast<MainWindow* >(gSelf);
                                        This line...

                                        Because now by using Invokemethod I am updating qlabel in main thread and not child or gstreamer thread that seems OK.

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

                                          I'd go a step further and pass directly the pointer to the QLabel object, it's even less instructions to use.

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

                                          V 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