【问题标题】:QT TreeWidgetItem Creation. Is this a Memory Leak and How should I code this properly?QTreeWidgetItem 创建。这是内存泄漏吗?我应该如何正确编码?
【发布时间】:2020-09-15 08:25:32
【问题描述】:

我正在加载一个文件,我需要为文件中的每个 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);



}

【问题讨论】:

    标签: c++ qt memory-leaks


    【解决方案1】:

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

    无论如何,只要在QTreeWidgetItem 的构造函数中提供一个父级:

    DisassemblyListItem = new QTreeWidgetItem(ui->DisassemblyTreeWidget);
    

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

    【讨论】:

    • 我是这么认为的,但想确定一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2013-08-12
    相关资源
    最近更新 更多