QListWidgetItem Destroyer and Release Memory
-
Best regards
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
-
@isaacEnrique
I don't follow, what is the issue? -
Hi
Do you mean
if you new your own object, say new MyData
and then put Mydata into widgetitem via setDataso 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.
-
@isaacEnrique
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.