Memory leak while using QOpenglWidget
-
I am testing a very minimal OpenGL setup using QOpenGLWidget in Qt 6.10 on Windows (MSVC 2022, 64-bit).
Even with a completely empty paintGL(), my application’s RAM usage increases continuously every time the widget repaints.I am trying to understand whether this is:
a Qt issue,
an OpenGL context / event loop issue, or
a NVIDIA driver leak (Threaded Optimization?).
When i disable Threaded Optimization in the nvidia settings , the leak dissappears.
class GLWidget : public QOpenGLWidget { Q_OBJECT public: explicit GLWidget(QWidget* parent = nullptr); void initializeGL() override; void paintGL() override; void resizeGL(int w, int h) override; protected: void resizeEvent(QResizeEvent* event) override; private: QTimer timer; }; ///////////////////////////////////////////////////////////////////////////////////////GLWidget::GLWidget(QWidget* parent) : QOpenGLWidget(parent) { QSurfaceFormat fmt; fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer); fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setVersion(4, 5); QSurfaceFormat::setDefaultFormat(fmt); connect(&timer, &QTimer::timeout, this, [this]() { update(); }); timer.setTimerType(Qt::PreciseTimer); timer.setInterval(0); // repaint as fast as possible timer.start(); } void GLWidget::initializeGL() { // nothing here } void GLWidget::resizeGL(int w, int h) { glViewport(0, 0, w, h); } void GLWidget::paintGL() { // EMPTY on purpose // glClearColor(0,0,0,1); // glClear(GL_COLOR_BUFFER_BIT); } -
An application consuming RAM doesn't necessarily mean there is a memory leak at all.
In your case, I believe there is no leak.One reason for increasing memory allocation is the Qt Backing Store. That can cause memory allocation if the a
QOpenGlWidgetis frequently resized. I can't say whether this is the case here, because the actual code causing the resizing/updating isn't shown. In any case: It's expected behaviour and not a memory leak: The memory is freed when the widget is deleted (provided that the class inheriting from it handles destruction well).Another likely cause can be the actual GPU driver. Some drivers defer freeing resources to whatever happens earlier: Context destruction or memory shortage.
-
I am testing a very minimal OpenGL setup using QOpenGLWidget in Qt 6.10 on Windows (MSVC 2022, 64-bit).
Even with a completely empty paintGL(), my application’s RAM usage increases continuously every time the widget repaints.I am trying to understand whether this is:
a Qt issue,
an OpenGL context / event loop issue, or
a NVIDIA driver leak (Threaded Optimization?).
When i disable Threaded Optimization in the nvidia settings , the leak dissappears.
class GLWidget : public QOpenGLWidget { Q_OBJECT public: explicit GLWidget(QWidget* parent = nullptr); void initializeGL() override; void paintGL() override; void resizeGL(int w, int h) override; protected: void resizeEvent(QResizeEvent* event) override; private: QTimer timer; }; ///////////////////////////////////////////////////////////////////////////////////////GLWidget::GLWidget(QWidget* parent) : QOpenGLWidget(parent) { QSurfaceFormat fmt; fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer); fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setVersion(4, 5); QSurfaceFormat::setDefaultFormat(fmt); connect(&timer, &QTimer::timeout, this, [this]() { update(); }); timer.setTimerType(Qt::PreciseTimer); timer.setInterval(0); // repaint as fast as possible timer.start(); } void GLWidget::initializeGL() { // nothing here } void GLWidget::resizeGL(int w, int h) { glViewport(0, 0, w, h); } void GLWidget::paintGL() { // EMPTY on purpose // glClearColor(0,0,0,1); // glClear(GL_COLOR_BUFFER_BIT); }@summit I think the problem is with your Timer because it is calling paintGL() constantly. In fact paintGL() Renders the OpenGL scene and should be called whenever the widget needs to be updated and not each time. I believe that each Rendering will use a part of the RAM and when you do that without freeing it the RAM will continue to Increase. You might consider calling a new Constructor of the widget to free the memory but it is just an hypothesis.
-
Related to https://bugreports.qt.io/browse/QTBUG-105886 ? Can't reproduce it on my system so I blame the graphics driver.
-
An application consuming RAM doesn't necessarily mean there is a memory leak at all.
In your case, I believe there is no leak.One reason for increasing memory allocation is the Qt Backing Store. That can cause memory allocation if the a
QOpenGlWidgetis frequently resized. I can't say whether this is the case here, because the actual code causing the resizing/updating isn't shown. In any case: It's expected behaviour and not a memory leak: The memory is freed when the widget is deleted (provided that the class inheriting from it handles destruction well).Another likely cause can be the actual GPU driver. Some drivers defer freeing resources to whatever happens earlier: Context destruction or memory shortage.
@Axel-Spoerl The memory keeps on increasing till the time system goes out of resource and the application crashes , but when i disable Threaded Optimization in Nvidia driver the same application does not increase the RAM.
-
Related to https://bugreports.qt.io/browse/QTBUG-105886 ? Can't reproduce it on my system so I blame the graphics driver.
@Christian-Ehrlicher This issue only occurs on workstation systems (in my case, an HP Z2 with an RTX A4000) and does not happen on laptops. I believe this may be due to the two systems using different branches of the NVIDIA driver. If i disable Threaded Optimization on the nvidia card then no leak happens. Also Drivers after 560XX.
-
@Axel-Spoerl The memory keeps on increasing till the time system goes out of resource and the application crashes , but when i disable Threaded Optimization in Nvidia driver the same application does not increase the RAM.
@summit Then it's an NVIDIA problem.