QTreeWidgetItem 创建。这是内存泄漏吗?我应该如何正确编码?

QT TreeWidgetItem Creation. Is this a Memory Leak and How should I code this properly?

我正在加载一个文件,我需要为文件中的每个 QT TreeWidgetItem 创建一行。因为我在 for 循环中执行此操作,所以我担心会发生内存泄漏。我 运行 Valgrind 有了这个,它似乎没有内存泄漏,但我是一般使用 Valgrind 的新手。我担心存在内存泄漏当填充 DisassemblyTreeWidget 时,我将调用

DisassemblyTreeWidget->clear();

当我完成该程序时,我将删除该对象,但我不确定 DisassemblyListItem 我认为它可能会丢失。

这是代码,我会尝试在 .欢迎在评论中提问。

如果这确实是内存泄漏,那么我应该如何编写一个循环来添加项目?

QTreeWidgetItem *DisassemblyListItem;

void SetDisassemblyWidgetContent(Ui::MainWindow *ui , std::string Address, std::string Mneumonics ,std::string Commment)
{

    const bool __sortingEnabled = ui->DisassemblyTreeWidget->isSortingEnabled();
    ui->DisassemblyTreeWidget->setSortingEnabled(false);
     //I worry Memory Leak exists here .

    DisassemblyListItem = new QTreeWidgetItem();
    DisassemblyListItem->setText(2, QApplication::translate("MainWindow", Commment.c_str(), Q_NULLPTR));
    DisassemblyListItem->setText(1, QApplication::translate("MainWindow", Mneumonics.c_str(), Q_NULLPTR));
    DisassemblyListItem->setText(0, QApplication::translate("MainWindow", Address.c_str(), Q_NULLPTR));

    ui->DisassemblyTreeWidget->addTopLevelItem(DisassemblyListItem);
    ui->DisassemblyTreeWidget->setSortingEnabled(__sortingEnabled);


}






void GetDisassemblyWidgetContent(Ui::MainWindow *ui)
{
   ui->DisassemblyTreeWidget->clear();
   std::string Address    = "";
   std::string Mneumonics = "";
   std::string Comment    = "";

  if( (BinaryType == ArchTypeELFX86 ) ||(BinaryType == ArchTypeELFX86) ){
       ui->DisassemblyTreeWidget->header()->resizeSection(0 /*column index*/, 250 /*width*/);
       ui->DisassemblyTreeWidget->header()->resizeSection(1 /*column index*/, 380 /*width*/);
  }else{
       ui->DisassemblyTreeWidget->header()->resizeSection(0 /*column index*/, 350 /*width*/);
       ui->DisassemblyTreeWidget->header()->resizeSection(1 /*column index*/, 470 /*width*/);
  }


    std::vector<std::string> DisassemblyWidgetJSONContent;
    std::string JsonReturnData = PyEngine_ExecuteCommandWithoutParams("pygdbmi-debugger", "GetTextSection");
    //qInfo() << JsonReturnData.c_str();
    DisassemblyWidgetJSONContent = SplitJsonIntoStringsEx(JsonReturnData);


    for (int i = 0 ; i < DisassemblyWidgetJSONContent.size(); i++)
    {
     auto JsonData = json::parse(DisassemblyWidgetJSONContent[i].c_str() );
      JsonData.at("Address").get_to(Address);
      JsonData.at("Mneumonics").get_to(Mneumonics);
      JsonData.at("Comment").get_to(Comment);
      SetDisassemblyWidgetContent(ui, Address, Mneumonics, Comment);
    }

    //Not sure if this is needed, I will check potom
   DisassemblyWidgetJSONContent.clear(); // Clear Vector
   std::vector<std::string>().swap(DisassemblyWidgetJSONContent);



}

我相信这里没有内存泄漏,因为 QTreeWidget 拥有添加的项目的所有权。 Qt 文档明确说明了关于 setItemWidget 函数。虽然 addTopLevelItem 函数的描述中没有直接解决所有权问题,但很可能它的工作原理是一样的。

无论如何要确保在 QTreeWidgetItem:

的构造函数中提供父级
DisassemblyListItem = new QTreeWidgetItem(ui->DisassemblyTreeWidget);

父级拥有所有子级的所有权并在销毁时自动删除它们。