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 while using QOpenglWidget
Forum Updated to NodeBB v4.3 + New Features

Memory leak while using QOpenglWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 463 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.
  • S Offline
    S Offline
    summit
    wrote last edited by summit
    #1

    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);
    }
    
    
    
    Ronel_qtmasterR 1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote last edited by
      #2

      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 QOpenGlWidget is 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.

      Software Engineer
      The Qt Company, Oslo

      S 1 Reply Last reply
      1
      • S summit

        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);
        }
        
        
        
        Ronel_qtmasterR Offline
        Ronel_qtmasterR Offline
        Ronel_qtmaster
        wrote last edited by
        #3

        @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.

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

          Related to https://bugreports.qt.io/browse/QTBUG-105886 ? Can't reproduce it on my system so I blame the graphics driver.

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

          S 1 Reply Last reply
          1
          • Axel SpoerlA Axel Spoerl

            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 QOpenGlWidget is 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.

            S Offline
            S Offline
            summit
            wrote last edited by
            #5

            @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.

            Axel SpoerlA 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Related to https://bugreports.qt.io/browse/QTBUG-105886 ? Can't reproduce it on my system so I blame the graphics driver.

              S Offline
              S Offline
              summit
              wrote last edited by summit
              #6

              @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.

              1 Reply Last reply
              0
              • S summit

                @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.

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote last edited by
                #7

                @summit Then it's an NVIDIA problem.

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                1

                • Login

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