如果不删除* itr,这会导致内存泄漏吗?我只是在想一个项目,不想拿走积分。
void Directory::removeFile(string fName)
{
// Get files from directory
list<Folder> folders = this->getFolders();
list<File> files = this->getFiles();
for (list<Folder>::iterator itr = folders.begin(); itr != folders.end(); ++itr)
{
if (fName == (*itr).getFolderName())
{
cout << fName << ": Is a directory." << endl;
return;
}
}
for (list<File>::iterator itr = files.begin(); itr != files.end(); ++itr)
{
if (fName == (*itr).getFileName())
{
cout << "Found it" << endl;
itr = files.erase(itr);
this->setFiles(files);
return;
}
}
cout << "This file does not exist." << endl;
}
编辑:之所以问这个原因是因为我做了valgrind并得到了这个结果。这是内存泄漏吗?
HEAP SUMMARY:
==27219== in use at exit: 0 bytes in 0 blocks
==27219== total heap usage: 25 allocs, 25 frees, 82,671 bytes allocated
==27219==
==27219== All heap blocks were freed -- no leaks are possible
==27219==
==27219== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==27219== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
答案 0 :(得分:0)
如果我不删除* itr,这会导致内存泄漏吗?
不,不会。
std::list::erase
将确保将调用该位置包含的对象的析构函数,在您的情况下为File
对象。