TOPIC: object modified during iteration
#1646
object modified during iteration 9 Months ago
This is an exception you may see from the latest version.

We recently discovered a problem if the hash table underlying an object or record grew or shrank while you were iterating over it. It did not happen often, but when it did the iteration could skip members or visit members twice.

To prevent this problem, object/record iterators now throw an "object modified during iteration" exception if you add or remove members during iteration. This is similar to Java "fail fast" iterators.

Note: It is ok to modify the value of a member. (Except in the rare case where modifying a value triggers rules which create new members.)

It it usually easy to fix. For example, in:

Code:

for m in ob.Members()
    if ...
        ob.Delete(m)



you would just need to add a .Copy()

Code:

for m in ob.Members().Copy()
    if ...
        ob.Delete(m)



Another good approach is to use methods like Map, Filter, Remove, RemoveIf instead of writing the loops yourself.

This is a relatively recent change, if you run into this error in stdlib code, please let us know.
 
 
andrew