Qt Forum

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

    Call for Presentations - Qt World Summit

    [SOLVED] Does QLinkedList destroy items on destructor call?

    General and Desktop
    3
    6
    1510
    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.
    • T3STY
      T3STY last edited by T3STY

      As title says, does QLinkedList destroy any stored items when the destructor is called? Or should I delete them manually, one by one, before destroying the QLinkedList ?

      The docs say that upon destructor call
      References to the values in the list, and all iterators over this list, become invalid
      but that doesn't explain it all...

      1 Reply Last reply Reply Quote 0
      • KiwiJeff
        KiwiJeff last edited by

        If you have a list of pointers, then yes, else the destructor takes care of it.

        JKSH 1 Reply Last reply Reply Quote 0
        • JKSH
          JKSH Moderators @KiwiJeff last edited by JKSH

          @KiwiJeff said:

          If you have a list of pointers, then yes, else the destructor takes care of it.

          No, it does not. That's what qDeleteAll() is for.

          @T3STY said:

          does QLinkedList destroy any stored items when the destructor is called? Or should I delete them manually, one by one, before destroying the QLinkedList ?

          Qt's containers do not call delete on their contents upon destruction, because that would be unsafe. Consider a scenario where multiple pointers point to the same object, and one of these pointers is in your list. If the list destructor destroys its contents, then all the other pointers would become dangling pointers. Often, that's not what people want.

          You must delete them manually, but you can use the qDeleteAll() convenience function. Alternatively, make your list store smart (shared) pointers instead of raw pointers

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          KiwiJeff 1 Reply Last reply Reply Quote 0
          • T3STY
            T3STY last edited by

            Thank you very much JKSH ;)

            1 Reply Last reply Reply Quote 0
            • KiwiJeff
              KiwiJeff @JKSH last edited by

              @JKSH said:

              @KiwiJeff said:

              If you have a list of pointers, then yes, else the destructor takes care of it.

              No, it does not. That's what qDeleteAll() is for.

              Thank you for providing a more explained post. However, my yes was pointed to his last question, not the title. My mistake for not using the quote.

              JKSH 1 Reply Last reply Reply Quote 0
              • JKSH
                JKSH Moderators @KiwiJeff last edited by

                @KiwiJeff said:

                Thank you for providing a more explained post. However, my yes was pointed to his last question, not the title. My mistake for not using the quote.

                Ah, I see. I'm sorry for misreading your post; I thought you said "Yes, the QList destructor takes care of deleting the items". Sorry again!

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post