使用qDeleteAll()删除QList项时无法防止内存泄漏

时间:2019-06-26 13:09:44

标签: c++ qt memory-leaks qlist

我定义了一个QList <QPushButton*>,我想稍后将此列表分配给QTableWidget。但是主要的问题就在这里,当我想删除所有QList项时,使用qDeleteAll()时会给我带来内存泄漏事件:

// Add QPushButton* pointers to the QList
for (int i = 0; i < 5000; i++)
{
    btnList.push_back(new QPushButton("delete"));
}

顺便说一句,除了第一种推指针的方法,我也尝试过这种方法:

// Add QPushButton* pointers to the QList with second approach
for (int i = 0; i < 5000; i++)
{
    QPushButton *btn = new QPushButton("delete");
    btnList.push_back(btn);
}

我尝试了两种方法来处理delete QList指针:

  1. 使用takeAt方法获取指针和delete指针:

    for (int i = 0; i < 5000; i++)
    {
        delete btnList.takeAt(0);
    }
    btnList.clear();
    
  2. 先使用qDeleteAll然后使用clear方法:

    // Delete all items
    qDeleteAll(btnList);
    btnList.clear();
    

但是内存泄漏仍然存在。我在 Visual Studio

上使用了msvc2015编译器

Also about my problem I found this link but it didnt help ed meThis link

0 个答案:

没有答案