tags:

views:

155

answers:

2

I get a segmentation fault when iterating over a set. The stack trace points to

std::_Rb_tree_const_iterator<Type>::operator++
std::_Rb_tree_increment()

but I get nothing more informative. The iterator is over a set returned by a function

for (FactSet::factset_iterator fact_it = (*binSet_it).getDependencyGraph().getExtentionalFactSet().begin();
                fact_it != (*binSet_it).getDependencyGraph().getExtentionalFactSet().end();
                ++fact_it) {...}

I cannot see the issue. Thanks in advance.

+1  A: 

You don't want to be iterating over the return value like that. The middle termination condition is re-evaluated every iteration, so your end() will be for a different set every time, which means your iterator will never reach it.

Cache the set in a local variable and then use the begin() and end() from that.

Peter Alexander
Thanks, this makes very good sense.I have tried a different approach:`for (FactSet::factset_iterator fact_it = (*binSet_it).getDependencyGraphRef().getExtentionalFactSetRef().begin();fact_it != (*binSet_it).getDependencyGraphRef().getExtentionalFactSetRef().end(); +fact_it) {...}`where getDependencyGraphRef and getExtentionalFactSetRef are returning constant references. This is working. I wanted to avoid copying as the structure I am dealing with is pretty big.
myahya
Peter Alexander
A: 

Do you erase elements within the loop? In that case you need to take the return value of the erase function (that gives iterator to the first element after the one(s) deleted) and you must not perform ++it after that round (that would skip the next element, or if it was already at the end, possibly cause a segfault in operator++).

Tronic