Reputation: 1241
I am currently working on an interface for an random application. In the interface I have a QListView
, which have three buttons related to it. The first one is add, which adds a new item on the widget, this button works fine. The problem comes with the clear button:
void NeuralNetwork::on_clearButton_clicked() {
this->model.get()->clear();
this->item.clear();
}
as you see both model and item are attributes of this class. They are declared as this:
std::unique_ptr<QStandardItemModel> model = unique_ptr<QStandardItemModel>(new QStandardItemModel());
vector<unique_ptr<QStandardItem>> item;
Initially I was using normal pointers, but as I searched on the internet, people were recommending using unique pointers to handle memory management.
I think there is something wrong with
this->model.get()
but I don't know exactly, since I have no experience with smart pointers. Note that the program doesn't get build errors, it works fine until I press the clear button on the interface, then the program crashes:
The program has unexpectedly finished.
Upvotes: 0
Views: 536
Reputation: 4076
Be carefull, as QStandardItemModel has ownership of standard items. Therefore you would get a double delete when you associated a standard item with a unique pointer.
In general, with Qt you don't need to use unique pointers, as parents already have ownership of their children.
Upvotes: 2