[Issue 4179] Deleting items from an associative array iterated over

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 29 09:51:45 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=4179


Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com


--- Comment #7 from Steven Schveighoffer <schveiguy at yahoo.com> 2011-03-29 09:47:53 PDT ---
It's unsafe.  The issue is that if you remove the element being iterated, it
goes back to the memory pool.

Here is the code that removes an element:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/aaA.d#L373

Note on line 402, the unused node is free'd using gc_free.

The only way I know to make it safe is letting the iteration routine remove the
current element at the right time .  Dcollections allows this.

The only way to solve this other than that is to keep a modification id, and
throw an error if the iteration discovers the array structure has been modified
during iteration.  A compiler error or warning will not help, because the
compiler cannot know whether a called function is going to modify the AA, or if
two AA references point to the same object.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list