我定义了一个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指针:
使用takeAt
方法获取指针和delete
指针:
for (int i = 0; i < 5000; i++)
{
delete btnList.takeAt(0);
}
btnList.clear();
先使用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