I am trying to list objects in a list widget in my GUI. I manage to show an item with these lines of code:
QListWidgetItem *currTest = new QListWidgetItem(QIcon("icons/lungs.jpg"), QString::number(respirationTests.size()));
ui->listWidget->addItem(currTest);
I learned this in this tutorial https://www.youtube.com/watch?v=2YRAJt-LbkM But I am worried since I use new without calling delete. is the QListWidgetItem object going to be deleted correctly when the UI main window is closed? I am not sure how qt manages memory with created widgets.
And also, just to make sure, if you restart your computer, the leaked memory from calling new without delete is going to be cleared, right?
Using new
without delete
in this case it's not going to result in a memory leak. Because Qt has a mechanism for it's OQbjects to be organized in a tree, where each parent is taking the ownership of it's children and will release the memory for the children when the parent's lifetime ends. In your particular case the listWidget
destructor will delete
all the items that you add/insert.
As for the second question: usually* the OS will take back the ownership of the memory when your application closes (*unless maybe if you work on some special cases, like some lower level OS internals and manage to leak memory from there). But nevertheless you should always make sure that your application has no resource leaks.