[SOLVED] Does QLinkedList destroy items on destructor call?



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



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


  • Moderators

    @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



  • Thank you very much JKSH ;)



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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.