Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Apparent memory leak with recursive QTimer::singleshot.

Apparent memory leak with recursive QTimer::singleshot.

Scheduled Pinned Locked Moved Unsolved Qt 6
7 Posts 3 Posters 1.4k Views
  • 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.
  • B Offline
    B Offline
    bcrew1375
    wrote on 17 Apr 2021, 03:27 last edited by bcrew1375
    #1

    I've tracked down a memory leak to a repeating singleshot timer. I'm not sure if the cause lies in Qt, is my own oversight, or something else, but I can reproduce it with a minimal example. I'm having this problem on a Windows 7 machine using Qt 6.0.0 and haven't been able to find anything addressing it online. So, the steps to reproduce are:

    1. Create a new Qt Widgets app.
    2. Create a function slot on MainWindow. Mine is called "executionLoop()"
    3. Call the function slot from the MainWindow constructor.
    4. Put the following code in the function:
    void MainWindow::executionLoop()
    {
        QTimer::singleShot(1, Qt::PreciseTimer, this, SLOT(executionLoop()));
    }
    

    The program will slowly but surely eat more RAM without ever freeing it. Unless I'm missing something, a singleshot timer should fire once and deallocate afterwards. Since the function is called once from outside and only recursively after that, there should only exist one singleshot timer at any given time. Anyone know what's going on?

    Clarification in case it wasn't obvious: this is in C++.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 17 Apr 2021, 12:14 last edited by
      #2

      Check with address sanitizer.

      How do you check RAM usage? If you are using System Monitor, it's very possible that each QTimer instance allocates new portion of RAM because you have free RAM available. Operating Systems often do it - the old instances are freed, but still the memory continues being "reserved" for your application. Only when there is not much RAM available, OS will start reusing old memory addresses.

      I'm not saying there is no leak here, I'm saying there maybe isn't one.

      (Z(:^

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bcrew1375
        wrote on 17 Apr 2021, 18:51 last edited by
        #3

        I'm using Task Manager's process monitor. I don't think that's the case here. I've had as much as 93% of RAM being used and the process continues to show more being used. The test program I made went from under 5 MB used to over 300 MB.

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 18 Apr 2021, 07:52 last edited by
          #4

          Please provide a minimal compilable example so we can reproduce the issue.

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

          B 1 Reply Last reply 18 Apr 2021, 09:21
          0
          • C Christian Ehrlicher
            18 Apr 2021, 07:52

            Please provide a minimal compilable example so we can reproduce the issue.

            B Offline
            B Offline
            bcrew1375
            wrote on 18 Apr 2021, 09:21 last edited by
            #5

            @Christian-Ehrlicher I'm not 100% sure this is a bug yet, but I made a bug report here already with a test project: https://bugreports.qt.io/browse/QTBUG-92912

            I've been letting it run for a while now and according to Task Manager, it's slowly climbed to using 2.3 GB of RAM.

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 18 Apr 2021, 11:08 last edited by
              #6

              Thx, but don't lease attach the binaries - they're not needed at a 8MB reproducer just scares the reader.

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

              B 1 Reply Last reply 18 Apr 2021, 16:51
              0
              • C Christian Ehrlicher
                18 Apr 2021, 11:08

                Thx, but don't lease attach the binaries - they're not needed at a 8MB reproducer just scares the reader.

                B Offline
                B Offline
                bcrew1375
                wrote on 18 Apr 2021, 16:51 last edited by bcrew1375
                #7

                @Christian-Ehrlicher Okay, my mistake. So, after letting the program run for about 24 hours straight. It reached about 3.5 GB used. I opened another application with high memory use that pushed the physical RAM usage to 98%. At this point, the application DID release about 2 GB and is now sitting at around 1.3 GB used, but it also caused the system to become unresponsive for some time, which was probably from the virtual memory swapping. Despite that, it seems like unacceptable and unexpected behavior for an application that simply runs a timer repeatedly to eventually take that much RAM.

                1 Reply Last reply
                0

                1/7

                17 Apr 2021, 03:27

                • Login

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