safely delete while iterating

sync
Volker Birk 2018-10-22 23:39:30 +02:00
parent c652ca1d7c
commit 233cfe4045
1 changed files with 3 additions and 4 deletions

View File

@ -53,9 +53,9 @@ public:
}
// Beware: does not delete *pdata nor *cdata! That's producer's and consumer's task!
void erase(typename Container::const_iterator pos)
typename Container::iterator erase(typename Container::const_iterator pos)
{
c.erase(pos);
return c.erase(pos);
}
// removes all elements with pdata==null && cdata==null
@ -67,11 +67,10 @@ public:
// clear the container. Delete all *pdata via custom deleter functor.
void clear(std::function<void(Pdata*)> deleter = [](Pdata *e) { delete e; })
{
for(auto q=begin(); q!=end(); ++q)
for(auto q=begin(); q!=end(); q=erase(q))
{
deleter(q->pdata);
q->pdata = nullptr;
erase(q);
}
}