Do AAs allocate on removal?

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Dec 7 22:11:48 PST 2013


On Sat, Dec 07, 2013 at 08:04:03PM +0100, Gerrit Wichert wrote:
[...]
>         private static void cleanObjects()
>         {
>             if (shouldCleanObjects) {
>                 foreach (key, val; objects) {
>                     if (!val) {
>                     objects.remove( key);
>                 }
>             }
>             shouldCleanObjects = false;
>           }
>         }
> 
>  Seems to work, but I'm not shure if it is save to remove entrys
> while iterating. Can't find any advise at this.
[...]

It's OK if you iterate over a *copy* of the AA keys, such as what you
get with .keys. Don't do it with .byKey, because it will lead to strange
behaviour and possibly crashes if you're not careful.

In general, it's unwise to delete things from a container while
iterating over it, unless the container was specifically designed to
support that. For details, see the comments on this issue:

	https://d.puremagic.com/issues/show_bug.cgi?id=10821


T

-- 
Obviously, some things aren't very obvious.


More information about the Digitalmars-d mailing list