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 QGraphicsScene.
Qt 6.11 is out! See what's new in the release blog

Memory leak in QGraphicsScene.

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 3.7k Views 1 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.
  • A Anton Shelenkov

    @JonB leak

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #4

    @Anton-Shelenkov
    Aren't you supposed to call scene->removeItem(item); if you want to delete it yourself, else the scene won't know it's gone/clean up resources properly, no? (That's why I thought your code would crash.) Mind, I've never used QGraphicsScene...

    A 1 Reply Last reply
    0
    • JonBJ JonB

      @Anton-Shelenkov
      Aren't you supposed to call scene->removeItem(item); if you want to delete it yourself, else the scene won't know it's gone/clean up resources properly, no? (That's why I thought your code would crash.) Mind, I've never used QGraphicsScene...

      A Offline
      A Offline
      Anton Shelenkov
      wrote on last edited by
      #5

      @JonB
      scene->removeItem(item);
      This don't work too :(

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

        Hi and welcome to devnet,

        removeItem removes the item from the scene, after that, it's the caller's responsibility to delete the item.

        What gives you that memory leak ?
        What OS are you running on ?
        What version of Qt are you using ?

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

        A 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          removeItem removes the item from the scene, after that, it's the caller's responsibility to delete the item.

          What gives you that memory leak ?
          What OS are you running on ?
          What version of Qt are you using ?

          A Offline
          A Offline
          Anton Shelenkov
          wrote on last edited by
          #7

          @SGaist
          This is smal test programm, and leak can be only hear.
          Windows 10.
          Qt 5.10.0.
          During the program I see in windows task manager how it requires more memory.

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

            You have an infinite loop where you allocate and delete stuff that chokes Qt's own event loop as it doesn't allow to run anything.

            Also, the OS doesn't necessarily reclaim immediately the freed memory.

            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
            3
            • A Anton Shelenkov

              @SGaist
              This is smal test programm, and leak can be only hear.
              Windows 10.
              Qt 5.10.0.
              During the program I see in windows task manager how it requires more memory.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #9

              @Anton-Shelenkov said in Memory leak in QGraphicsScene.:

              During the program I see in windows task manager how it requires more memory.

              That's not how to test properly for a memory leak.
              It did not occur to me that the code was the actual program you were running --- I assumed it was a "skeleton" representing the code you were actually running!

              A 1 Reply Last reply
              0
              • JonBJ JonB

                @Anton-Shelenkov said in Memory leak in QGraphicsScene.:

                During the program I see in windows task manager how it requires more memory.

                That's not how to test properly for a memory leak.
                It did not occur to me that the code was the actual program you were running --- I assumed it was a "skeleton" representing the code you were actually running!

                A Offline
                A Offline
                Anton Shelenkov
                wrote on last edited by
                #10

                @JonB
                If i write endless cycle with other class it work correct:

                while(true)
                {
                    QString *str = new QString;
                    delete str;
                   //or
                

                /*
                A *empty_class = new A;
                delete empty_class;
                */
                }

                JonBJ 1 Reply Last reply
                0
                • A Anton Shelenkov

                  @JonB
                  If i write endless cycle with other class it work correct:

                  while(true)
                  {
                      QString *str = new QString;
                      delete str;
                     //or
                  

                  /*
                  A *empty_class = new A;
                  delete empty_class;
                  */
                  }

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #11

                  @Anton-Shelenkov
                  Yes, but these examples are simply not as complex as what QGraphicsScene::addItem() is doing, at least book-keeping-wise, behind the scenes, e.g. "taking ownership" of the item --- how do you know what that involves?

                  At absolute minimum: firstly, you must try:

                      for(auto item : scene->items())
                      {
                          scene->removeItem(item);
                          delete item;
                      }
                  

                  And secondly, exactly as @SGaist says, you must address:

                  You have an infinite loop where you allocate and delete stuff that chokes Qt's own event loop as it doesn't allow to run anything.

                  e.g. at least allow Qt event loop to run suitably.

                  A 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Anton-Shelenkov
                    Yes, but these examples are simply not as complex as what QGraphicsScene::addItem() is doing, at least book-keeping-wise, behind the scenes, e.g. "taking ownership" of the item --- how do you know what that involves?

                    At absolute minimum: firstly, you must try:

                        for(auto item : scene->items())
                        {
                            scene->removeItem(item);
                            delete item;
                        }
                    

                    And secondly, exactly as @SGaist says, you must address:

                    You have an infinite loop where you allocate and delete stuff that chokes Qt's own event loop as it doesn't allow to run anything.

                    e.g. at least allow Qt event loop to run suitably.

                    A Offline
                    A Offline
                    Anton Shelenkov
                    wrote on last edited by
                    #12

                    Thanks all so much, i understood how to do it.
                    @SGaist I made the loop not infinite and the problem disappeared you were right.

                    JonBJ 1 Reply Last reply
                    0
                    • A Anton Shelenkov

                      Thanks all so much, i understood how to do it.
                      @SGaist I made the loop not infinite and the problem disappeared you were right.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #13

                      @Anton-Shelenkov said in Memory leak in QGraphicsScene.:

                      Thanks all so much, i understood how to do it.
                      @SGaist I made the loop not infinite and the problem disappeared you were right.

                      Scarily, it seems that @SGaist is always right! ;-)

                      1 Reply Last reply
                      2

                      • Login

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