monotone-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Monotone-devel] set's erase() and iterators


From: Václav Haisman
Subject: Re: [Monotone-devel] set's erase() and iterators
Date: Tue, 16 Oct 2007 22:42:39 +0200
User-agent: Thunderbird 2.0.0.6 (Windows/20070728)


Markus Schiltknecht wrote, On 16.10.2007 21:33:
> Hello Will,
> 
> on my box, monotone's suspend test reproducibly failed. Today I've
> tracked down the bug, which took me quite some time.
> 
> When erasing from a set<>, most iterators remain valid, except the one
> you've just erased (of course!). But that in turn means, that the
> following code in project.cc could lead to a segmentation fault
> (probably also dependent on the STL implementation):
> 
>   for(std::set<revision_id>::iterator it = branch.second.begin();
>       it != branch.second.end(); ++it)
>     if (some_decision_func(...))
>       branch.second.erase(*it);
> 
> It's one of the less obvious things in the STL, and about everybody has
> stumbled over it before, at least I did already several times ;-)
> 
> One option to circumvent that problem is recreating the set, as I did
> for project.cc now. Another option might be using the STL algorithm
> remove_if().
I think that you cannot use std::remove_if() on std::set<> since it is not a
sequence as the standard understands the term. And recreating the whole set
seems wasteful. Instead you could rewrite the loop into while() loop and use
something like

while (it != set.end ())
{
  if (condition ())
  {
    iterator tmp_it = it++;
    set.erase (tmp_it);
    continue;
  }

  // more code

  ++it;
}       

> 
> Regards
> 
> Markus

--
VH

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]