Browse Source

safely delete while iterating

sync
Volker Birk 7 years ago
parent
commit
233cfe4045
  1. 7
      pc_container.hh

7
pc_container.hh

@ -53,9 +53,9 @@ public:
} }
// Beware: does not delete *pdata nor *cdata! That's producer's and consumer's task! // 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 // removes all elements with pdata==null && cdata==null
@ -67,11 +67,10 @@ public:
// clear the container. Delete all *pdata via custom deleter functor. // clear the container. Delete all *pdata via custom deleter functor.
void clear(std::function<void(Pdata*)> deleter = [](Pdata *e) { delete e; }) 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); deleter(q->pdata);
q->pdata = nullptr; q->pdata = nullptr;
erase(q);
} }
} }

Loading…
Cancel
Save