Re: Possible GHashTable bug?



Derek Simkowiak <dereks realloc net> writes:
> 
> 	Ooh, ouch; I take this means I also cannot *add* elements while
> within g_hash_table_foreach().  That may be a showstopper for me.  How can
> player1 fire a rocket (resulting in a new entity --the rocket-- for the
> hash) if I can't add entities in the iteration of my main loop?  I guess
> I'll need a queue or something for post-foreach additions and removals.
> 
> 	Anyway, thanks for the help.  It would be cool if there were an
> Iterator for the GHashTable (g_hash_table_next() or some such) that would
> still allow you to add/remove elements between calls.
> 

Many of the C++ STL containers (for example) have this same property
that you can't mutate during iteration.

The usual solution is to flatten the hash into a list, then iterate
over that.

(You want to do this from a correctness standpoint anyway - if you
insert/remove during iteration, then it becomes undefined whether you
then iterate over what you inserted/removed, and infinite loops or
failure to move over some values become possibilities.)

Havoc





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