Unsolved Memory leak in QGraphcisPixmapItem.setPixmap()?
-
Hi and welcome to devnet,
Why are you creating your
im
on the heap ? There's no need for that. -
@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 codegi->setPixmap(QPixmap::fromImage(*im, Qt::NoFormatConversion));
should be correct, right?
-
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.
-
@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 codeprotected: 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?
-
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.
-
@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 OpenGLQGLWidget* 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).
-
Can you get a stack trace from that ?
-
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.
-
@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.. -
Seeing QGLWidget I didn't realise that you were using Qt 5... QGLWidget has been deprecated in favour of QOpenGLWidget.