Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QListWidgetItem Destroyer and Release Memory
In the event that the data attribute of a QListWidgetItem is put to a pointer to an object created dynamically (with new): The object in question is destroyed, and the corresponding memory is released when the destroyer of the QListWidgetItem is invoked?
Thanks for any responses and/or comments
I don't follow, what is the issue?
Do you mean
if you new your own object, say new MyData
and then put Mydata into widgetitem via setData
so the variant holds a pointer to Mydata
Then it will not be deleted (as far as i can see)
when QListWidgetItem is deleted.
@kshegunov My post is not a issue, rather it is a practice question. The answer will tell me if I should release the memory myself (with 'delete') or if I can relax and trust the destroyer of QListWidgetItem.
@mrjj Certainly, I thought so. The documentation did not seem clear about
Well since its stored in a QVariant, it means that
it can be any value. so it cannot know if a pointer or a "direct" type so
would be hard for it to delete it.
But, you might be able to wrap the pointer in a scope smart pointer and when scoped is deleted pr auto, then it deletes the actual Myclass also.
You need to release the memory on your own if data you placed inside QListWidgetItem is allocated using new.