Fortify doesn't like QListWidget::addItem(new QListWidgetItem) and reports a false memory leak, even though QT manages the memory properly.
I'm trying to figure out a work-around.
I was told to use a std::shared_ptr, but I haven't figured out the syntax yet.
Here's what I've got so far, but it reports an error about the type.
These 2 lines of code are all I need to fix, there is no further context. Just looking for the syntax for a shared pointer to QListWidgetItem, adding the item to the list widget with addItem().
Any syntax that works is fine. MUST create a QListWidgetItem and THEN add it. Cannot use additem("string") syntax.
In a header file, declare member variable item:
...
class Class1{
...
std::shared_ptr<QListWidgetItem> item;
...
};
In a source file:
...
Class1::ClassFunction1()
{
std::make_shared<QListWidgetItem> item("AStringToAdd");
ui->qlw->addItem(item);
}
This might do the trick, based on code you show in your question:
That way,
itemwill go out of scope beforeui, and will be deleted by theunique_ptr. When the item is deleted, it will notify the view, and view will remove the item.If you do it the other way around, view will delete the item, but it has no way to notify the
unique_ptr. Thereforeunique_ptrwill delete it again, resulting in Undefined Behavior, with luck just a crash.