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. Cleaning up QVector/QContainers?
QtWS25 Last Chance

Cleaning up QVector/QContainers?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.3k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    When I delete obejct that had QVector<item>/QVector<QMap<item,int>> etc etc assuming the items are not *, do I need to do anything to vectors in ~delete of object holding them or are they automatically cleared/memory released? I'm trying to understand my memory leak :- )

    1 Reply Last reply
    0
    • D Dariusz

      @Christian-Ehrlicher Ok so I don't have to do the ~ob(){} example above in objects at all as all will automatically clear up ?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @Dariusz said in Cleaning up QVector/QContainers?:

      as all will automatically clear up ?

      Yes (again)

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

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

        When a container holds objects, the object is deleted when the container is cleared (not Qt-specific)

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

        1 Reply Last reply
        4
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #3

          Adding to @Christian-Ehrlicher the object in the container shall have a proper destructor not creating memory leaks itself.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          2
          • D Offline
            D Offline
            Dariusz
            wrote on last edited by Dariusz
            #4

            So I though, but as far as I remember... .clear() does not release memory. I have to call something else afterwards to actually release memory. Which make me wonder... if I have to call it in destructor as well ?

            Say

            class ob{
            QVector<int>ints;
            QVector<otherOB> otherObs
            
            ~ob(){
            ints.clear();
            ints.squeeze();
            otherObs.clear();
            otherObs.squeeze();
            }
            

            ?

            kshegunovK 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Dariusz said in Cleaning up QVector/QContainers?:

              indestructor as wel

              No, why should you? The container is destroyed automatically in the dtor - c++ basics.

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

              D 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                @Dariusz said in Cleaning up QVector/QContainers?:

                indestructor as wel

                No, why should you? The container is destroyed automatically in the dtor - c++ basics.

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #6

                @Christian-Ehrlicher Ok so I don't have to do the ~ob(){} example above in objects at all as all will automatically clear up ?

                Christian EhrlicherC 1 Reply Last reply
                0
                • D Dariusz

                  @Christian-Ehrlicher Ok so I don't have to do the ~ob(){} example above in objects at all as all will automatically clear up ?

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Dariusz said in Cleaning up QVector/QContainers?:

                  as all will automatically clear up ?

                  Yes (again)

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

                  1 Reply Last reply
                  1
                  • D Dariusz

                    So I though, but as far as I remember... .clear() does not release memory. I have to call something else afterwards to actually release memory. Which make me wonder... if I have to call it in destructor as well ?

                    Say

                    class ob{
                    QVector<int>ints;
                    QVector<otherOB> otherObs
                    
                    ~ob(){
                    ints.clear();
                    ints.squeeze();
                    otherObs.clear();
                    otherObs.squeeze();
                    }
                    

                    ?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #8

                    @Dariusz said in Cleaning up QVector/QContainers?:

                    So I though, but as far as I remember... .clear() does not release memory. I have to call something else afterwards to actually release memory. Which make me wonder... if I have to call it in destructor as well ?

                    clear() releases the memory used by the QVector. If you put pointers in there, then it's going to free the memory the pointers occupied, not the memory the pointers reference - that'd be your job.
                    In all ways QVector<> is behaving like any other RAII-compliant type.

                    Calling clear() is not necessary, nor is calling squeeze(), the vector does that automatically when it destructs. Here's a relevant paragraph: http://eel.is/c++draft/stmt.dcl#2

                    Read and abide by the Qt Code of Conduct

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

                      Hi,

                      To delete the content there's qDeleteAll.

                      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

                      • Login

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