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. [SOLVED] Does QLinkedList destroy items on destructor call?
QtWS25 Last Chance

[SOLVED] Does QLinkedList destroy items on destructor call?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.8k 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by T3STY
    #1

    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
    0
    • KiwiJeffK Offline
      KiwiJeffK Offline
      KiwiJeff
      wrote on last edited by
      #2

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

      JKSHJ 1 Reply Last reply
      0
      • KiwiJeffK KiwiJeff

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

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #3

        @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

        KiwiJeffK 1 Reply Last reply
        0
        • T3STYT Offline
          T3STYT Offline
          T3STY
          wrote on last edited by
          #4

          Thank you very much JKSH ;)

          1 Reply Last reply
          0
          • JKSHJ 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

            KiwiJeffK Offline
            KiwiJeffK Offline
            KiwiJeff
            wrote on last edited by
            #5

            @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.

            JKSHJ 1 Reply Last reply
            0
            • KiwiJeffK KiwiJeff

              @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.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @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
              0

              • Login

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