Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. App hangs in QPixmap::fromImage
Qt 6.12 LTS is out! See what's new in the release blog

App hangs in QPixmap::fromImage

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 6 Posters 17.2k Views 2 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
    mpouchol
    wrote on last edited by
    #1

    Hi,

    I'm using a Qt app to display video stream from a grayscale devicealab camera. Images are displayed in a QGraphicsView using QPixmap. For some reason app randomly gets stuck in QPixmap::fromImage!

    Environment:
    Qt 5.15.0 msvc2019 x64

    Execution flow:

    • acquire images in camera thread
    • in UI thread, at refresh rate make a copy of image buffer, then create a QImage from buffer data
    • create QPixmap from QImage
    • set QPixmap to a QGraphicsPixmapItem

    Call stack:
    Using Qt's dlls with pdb I see that Qt waits for a semaphore to be released after image buffer conversion:
    71623d05-7ef5-4785-b8a8-c69ad89aee5e-image.png

    Image conversion:
    From what I see in Qt codebase, the convert_generic function uses a thread pool to perform image buffer conversion. One of the threads doesn't release the semaphore so UI hangs.

    Any help greatly appreciated.
    Thanks

    JonBJ 1 Reply Last reply
    0
    • M mpouchol

      Hi,

      I'm using a Qt app to display video stream from a grayscale devicealab camera. Images are displayed in a QGraphicsView using QPixmap. For some reason app randomly gets stuck in QPixmap::fromImage!

      Environment:
      Qt 5.15.0 msvc2019 x64

      Execution flow:

      • acquire images in camera thread
      • in UI thread, at refresh rate make a copy of image buffer, then create a QImage from buffer data
      • create QPixmap from QImage
      • set QPixmap to a QGraphicsPixmapItem

      Call stack:
      Using Qt's dlls with pdb I see that Qt waits for a semaphore to be released after image buffer conversion:
      71623d05-7ef5-4785-b8a8-c69ad89aee5e-image.png

      Image conversion:
      From what I see in Qt codebase, the convert_generic function uses a thread pool to perform image buffer conversion. One of the threads doesn't release the semaphore so UI hangs.

      Any help greatly appreciated.
      Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @mpouchol said in App hangs in QPixmap::fromImage:

      in UI thread, at refresh rate make a copy of image buffer, then create a QImage from buffer data

      Hello and welcome.

      May not be the issue/relevant, but just checking: how do you "make a copy of image buffer" safely, given that camera thread is updating image buffer? Is that code thread-safe?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpouchol
        wrote on last edited by
        #3

        Hello @JonB ,

        This copy is protected with a mutex.
        The camera thread itself keeps on getting images from the camera, only the main thread is frozen.

        1 Reply Last reply
        1
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Look in your other threads where you also try to convert a QImage and hang somewhere near convert_generic()

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

          M 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Look in your other threads where you also try to convert a QImage and hang somewhere near convert_generic()

            M Offline
            M Offline
            mpouchol
            wrote on last edited by
            #5

            @Christian-Ehrlicher the QImage to QPixmap conversion is only done in the main/UI thread.

            Christian EhrlicherC 1 Reply Last reply
            0
            • M mpouchol

              @Christian-Ehrlicher the QImage to QPixmap conversion is only done in the main/UI thread.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mpouchol said in App hangs in QPixmap::fromImage:

              the QImage to QPixmap conversion is only done in the main/UI thread.

              This does not what I've suggested... go through your other threads and take a look what they're doing and if there's another one in this function.

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

              M 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @mpouchol said in App hangs in QPixmap::fromImage:

                the QImage to QPixmap conversion is only done in the main/UI thread.

                This does not what I've suggested... go through your other threads and take a look what they're doing and if there's another one in this function.

                M Offline
                M Offline
                mpouchol
                wrote on last edited by
                #7

                @Christian-Ehrlicher thanks for your suggestion. I'm not sure to understand what I should do.
                The convert_generic function is actually one of the function called by QPixmap::fromImage as you can see in the callstack attached to my question.
                The app is stuck in this function trying to acquire a semaphore.

                JonBJ 1 Reply Last reply
                0
                • M mpouchol

                  @Christian-Ehrlicher thanks for your suggestion. I'm not sure to understand what I should do.
                  The convert_generic function is actually one of the function called by QPixmap::fromImage as you can see in the callstack attached to my question.
                  The app is stuck in this function trying to acquire a semaphore.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @mpouchol
                  @Christian-Ehrlicher is asking you to grep all your code for every occurrence of a call to fromImage() and/or convertToFormat() to see whether you call it in more than one place.

                  M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @mpouchol
                    @Christian-Ehrlicher is asking you to grep all your code for every occurrence of a call to fromImage() and/or convertToFormat() to see whether you call it in more than one place.

                    M Offline
                    M Offline
                    mpouchol
                    wrote on last edited by
                    #9

                    Thanks @JonB, I only use QPixmap::fromImage in the main thread. I do not call convertToFormat() directly, it's called in fromImage.

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #10

                      Is your QImage good? Does saving the QImage to a file work? What is the QImage::Format of the image?

                      M 1 Reply Last reply
                      0
                      • M mchinand

                        Is your QImage good? Does saving the QImage to a file work? What is the QImage::Format of the image?

                        M Offline
                        M Offline
                        mpouchol
                        wrote on last edited by
                        #11

                        I not sure about the QImage, I will try to save it. It's mostly a temporary wrapper around the buffer data that's used to create a QPixmap. The format is QImage::Format_Grayscale8

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mchinand
                          wrote on last edited by
                          #12

                          Also, does it sometimes work and sometimes hang or always hang? Are you possibly not getting a full frame of data from the camera? Each frame of data should be the same size (number of bytes), right?

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mpouchol
                            wrote on last edited by
                            #13

                            It works most of the time and hangs randomly. It can take somewhere between a few seconds and a 1-2 hours to hang. Images are fetched from camera at around 60fps and each frame should be the same size. I suspect data gets corrupted somewhere but I was expecting a crash instead of an app freeze. It's a bit strange that it always hang at the same point.

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

                              Again: it hangs on a mutex lock - nothing to do with the data. Take a look if this mutex is locked in another thread...

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

                              M 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                Again: it hangs on a mutex lock - nothing to do with the data. Take a look if this mutex is locked in another thread...

                                M Offline
                                M Offline
                                mpouchol
                                wrote on last edited by
                                #15

                                @Christian-Ehrlicher the mutex is owned by Qt code inside QPixmap::fromImage
                                I don't use this function from other threads so I don't see what I did that caused it to stay locked.

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • M mpouchol

                                  @Christian-Ehrlicher the mutex is owned by Qt code inside QPixmap::fromImage
                                  I don't use this function from other threads so I don't see what I did that caused it to stay locked.

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @mpouchol said in App hangs in QPixmap::fromImage:

                                  I don't use this function from other threads so I don't see what I did that caused it to stay locked.

                                  I'm giving up... looks like it's too hard to look into the other threads while it is hanging to see if another thread is also using this mutex. So try to figure it out by your own.

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

                                  M 1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    @mpouchol said in App hangs in QPixmap::fromImage:

                                    I don't use this function from other threads so I don't see what I did that caused it to stay locked.

                                    I'm giving up... looks like it's too hard to look into the other threads while it is hanging to see if another thread is also using this mutex. So try to figure it out by your own.

                                    M Offline
                                    M Offline
                                    mpouchol
                                    wrote on last edited by
                                    #17

                                    @Christian-Ehrlicher said in App hangs in QPixmap::fromImage:

                                    I'm giving up... looks like it's too hard to look into the other threads

                                    I did that and that's not the case. Thanks for your help anyway.

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

                                      Ok, taking a deeper look into convert_generic() the qimage is converted with more than one thread. One of these subthreads hangs maybe due to a broken QImage. Look into your other threads to see which one is somewhere hanging inside convertSegment()

                                      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
                                      • M Offline
                                        M Offline
                                        mpouchol
                                        wrote on last edited by
                                        #19

                                        I also had a look at the code inside convert_generic(). When the main thread is frozen I don't see other threads hanging in convertSegment which make me think that maybe one of the convertSegment threads has died silently and is not releasing the semaphore in convert_generic because I don't see visual studio raising exceptions in other threads.

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

                                          If you compile Qt by your own build a big try/catch block around convert_segment and simply catch all exceptions to see if there is one.
                                          You can also create a helper thread which writes out the current qimage after a certain period of time when it was not canceled in between so you can inspect the qimage.

                                          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
                                          0

                                          • Login

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