Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Cleaning up QVector/QContainers?

    General and Desktop
    5
    9
    664
    Loading More Posts
    • 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
      Dariusz last edited by

      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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion @Dariusz last edited by

        @Dariusz said in Cleaning up QVector/QContainers?:

        as all will automatically clear up ?

        Yes (again)

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 1
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

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

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 4
          • K
            koahnig last edited by

            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 Reply Quote 2
            • D
              Dariusz last edited by 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();
              }
              

              ?

              kshegunov 1 Reply Last reply Reply Quote 0
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                @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 has to stay free or it will die.

                D 1 Reply Last reply Reply Quote 2
                • D
                  Dariusz @Christian Ehrlicher last edited by

                  @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 Ehrlicher 1 Reply Last reply Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion @Dariusz last edited by

                    @Dariusz said in Cleaning up QVector/QContainers?:

                    as all will automatically clear up ?

                    Yes (again)

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 1
                    • kshegunov
                      kshegunov Moderators @Dariusz last edited by

                      @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 Reply Quote 4
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 3
                        • First post
                          Last post