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. Memory leak in QGraphcisPixmapItem.setPixmap()?
Forum Updated to NodeBB v4.3 + New Features

Memory leak in QGraphcisPixmapItem.setPixmap()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Why are you creating your im on the heap ? There's no need for that.

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

    N 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

      Why are you creating your im on the heap ? There's no need for that.

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

      @SGaist
      yes, i know, but for this particular issue it does not matter.
      Can You please give me advice how properly setPixmap() to QGraphicsPixmapItem()?
      I belive this code

      gi->setPixmap(QPixmap::fromImage(*im, Qt::NoFormatConversion));
      

      should be correct, right?

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

        On a second look, one thing is that calling bits like that triggers a deep copy of the pixel data which is a bad idea.

        If you want to be sure that there's a leak at some point you should really create a minimal compilable example that contains the minimum code to trigger the problem.

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

        N 1 Reply Last reply
        0
        • SGaistS SGaist

          On a second look, one thing is that calling bits like that triggers a deep copy of the pixel data which is a bad idea.

          If you want to be sure that there's a leak at some point you should really create a minimal compilable example that contains the minimum code to trigger the problem.

          N Offline
          N Offline
          nckma
          wrote on last edited by
          #5

          @SGaist thank You for Your reply, I really do not know whom can I ask.
          Besdies that I do not think this is a deep copy issue.
          I have just commented out all bits() related code

          protected:
             virtual void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE
             {
                  /*
                 static uchar c = 0;
                 c=c+0x20;
                 uchar* pbits = im->bits();
                 for(int i=0; i<1024*1024; i++)
                 {
                     pbits[i*4+0] =0xFF;
                     pbits[i*4+1] =c;
                     pbits[i*4+2] =0xFF;
                     pbits[i*4+3] =0xFF;
                 }
                 */
                 gi->setPixmap(QPixmap::fromImage(*im, Qt::NoFormatConversion));
             }
          

          But I see issue is still there.
          Still huge memory leaks during mouse press and move.

          Second thing. Issue is very simple to avoid - just comment out OpenGL viewport:

              GraphicsView window(&scene);
              //QGLWidget* gl = new QGLWidget();
              //window.setViewport(gl);
          

          And that is all - no more memory leaks. This cannot be related to QImage.bits(), right?

          3rd thing. Issue happens only on some machines, not on every.
          I have one machine with windows 8 where this happens.
          On other Windows 8 machine this does not happen. On 3rd machine with windows10 this does not happen.

          I begin to think that this is either particular machine OpenGL drivers issue or missing some Windows updates related to graphcis. Can this be true?

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

            From your description, the driver installed could indeed be at fault. Application using OpenGL depends heavily on the drivers installed and this is nothing Qt specific. Thus if there's a bug in the driver, then application will appear as being buggy though they likely be innocent.

            Note that doing heavy processing and setting setting a pixmap in a function like mouseMoveEvent isn't a nice thing to do, this event will be triggered many times and quickly so you might be swamping your application yourself doing that.

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

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              From your description, the driver installed could indeed be at fault. Application using OpenGL depends heavily on the drivers installed and this is nothing Qt specific. Thus if there's a bug in the driver, then application will appear as being buggy though they likely be innocent.

              Note that doing heavy processing and setting setting a pixmap in a function like mouseMoveEvent isn't a nice thing to do, this event will be triggered many times and quickly so you might be swamping your application yourself doing that.

              N Offline
              N Offline
              nckma
              wrote on last edited by
              #7

              @SGaist well,
              I have installed into VMWARE virtual machine fresh Windows 8.1 N - Updated from MSDN.
              VMWARE tools are installed.
              Trying same example with enabled OpenGL

              QGLWidget* gl = new QGLWidget();
              window.setViewport(gl);
              

              Any setPixmap() to QGraphicsPixmapItem on scene causes memory leak.
              Not setPixmap() exactly but maybe events triggered by setPixmap().
              Disabling OpenGL helps.

              I do not see any possibility to modify QGraphcisPixmapItem on scene on the fly.

              On windows 10 I do not see such issue, neither on real HW computer nor in VMWARE machine with win10.
              Feel myself stupid - cannot resolve this issue.

              Bad thing is that application already distributed between users and some of them complain on crash (out of memory).

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

                Can you get a stack trace from that ?

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

                N 1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #9

                  A leak in the OpenGL driver is a real possibility. I had one of those many years ago, and was lucky to get a fix by the graphics card vendor. Observe whether the leak occurs always on cards from the same chipset vendor. Consider that even VMWare has to rely on the underlying hardware drivers at some point, otherwise drawing would be rather slow.

                  Have you tried upgrading your VM (on which the leak occurs) to Win10? Does the leak occur then?

                  Back then, I decided to run my software without any hardware acceleration. I was lucky the performance was sufficient, so I could rely on the application to behave the same way on all target systems.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Can you get a stack trace from that ?

                    N Offline
                    N Offline
                    nckma
                    wrote on last edited by
                    #10

                    @SGaist
                    I hope I found solution for bug in my app.
                    If I use

                    //QGLWidget* gl = new QGLWidget();
                    QOpenGLWidget* gl = new QOpenGLWidget();
                    window.setViewport(gl);
                    

                    then I do not see memory leaks in my app on WIndows 8.1.
                    One line of code changes everything..
                    I will do more tests on side effects..

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

                      Seeing QGLWidget I didn't realise that you were using Qt 5... QGLWidget has been deprecated in favour of QOpenGLWidget.

                      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
                      0

                      • Login

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