Solved Removing custom widget from QList, will it be deleted from memory?
-
Hi,
This is a quick question, I am still working on my C++ and QT skills, this is the thing, I created a list of custom widgets,
QList<cWidget*> list; then I created a function to add the widgets:parent::addwidget(int id){ cWidget *p_widget = new cWidget(id, this); list.append(p_widget); }
If I use list.removeAt(id) will delete the *p_widget from memory too?
Thanks
-
@SGaist said in Removing custom widget from QList, will it be deleted from memory?:
It's not the job of QList to manage the memory of an object behind a pointer, that is yours.
This is the important thing. The fact you put the object into/take it out of a
QList<>
simply has no effect on the situation if there were no list.You went:
cWidget *p_widget = new cWidget(id, this);
so in principle it's your responsibility to delete it. In this case, if you have attached it to the parent by passing
this
, so alternatively it will get deleted if/when that gets deleted. -
Hi,
No, there's no reason for it to delete anything. You just remove the pointer from the list. It has nothing to do with the object being pointed at.
-
@SGaist thanks, the obvious question would be, what about the memory where those objects are being stored? should it be free?
-
Do you mean the memory used to store the pointer internally in your a QList ?
-
No, the memory of object when it was created. I assuming that QList can handle its memory resources properly.
-
It's not the job of QList to manage the memory of an object behind a pointer, that is yours.
-
@Jc_Opc said in Removing custom widget from QList, will it be deleted from memory?:
No, the memory of object when it was created. I assuming that QList can handle its memory resources properly.
We might be talking of two different things please clarify what you have in mind with a code sample.
-
@SGaist said in Removing custom widget from QList, will it be deleted from memory?:
It's not the job of QList to manage the memory of an object behind a pointer, that is yours.
This is the important thing. The fact you put the object into/take it out of a
QList<>
simply has no effect on the situation if there were no list.You went:
cWidget *p_widget = new cWidget(id, this);
so in principle it's your responsibility to delete it. In this case, if you have attached it to the parent by passing
this
, so alternatively it will get deleted if/when that gets deleted. -
thank you all, that was the reason why I put
this
, but bear with me a little more, I just want to know how to manage memory heap properly when using a containers (good practices). This is my attempt deleteWidget() function assuming that I am not usingthis
when creating the widget so I would do:After leaving the addWidget function, I only have the list of widgets, so here it is:
void parent::deleteWidget(int id){ delete list.at(id); list.removeAt(id); }
The reason of my confusion is because I always created each widget with its own pointer name, in here the name is the same but stored dynamically in a container.
-
@Jc_Opc said in Removing custom widget from QList, will it be deleted from memory?:
void parent::deleteWidget(int id){
delete list.at(id);
list.removeAt(id);
}void parent::deleteWidget(int id){ QWidget *widget = list.takeAt(id); delete widget; }
Would be cleaner. You can even make it a one liner.
One unrelated thing: please use meaningful name for classes and variables. It will make your code easier to work with.
-
@SGaist Thanks, in my code the names are different, and are kind of harder to understand based on the context, I am still learning how to name objects properly though; looking at others codes and practice will do the trick. In here I just wanted to learn the principles and your answer and @JonB's helped me a lot.
The time you guys take to answer all these questions are very appreciated.