[Gambas-devel] Free state memory when enumeration BREAKs
Benoît Minisini
gambas at ...1...
Tue Aug 28 18:07:00 CEST 2012
Le 28/08/2012 17:25, Tobias Boege a écrit :
> Hi,
>
> for my enumeration state, I allocate a buffer via GB.Alloc(). If it happens
> that the enumeration gets aborted by means of BREAK, is there any
> notification mechanism?
>
> And just to be sure I well understood: GB.ListEnum(), GB.NextEnum() and
> GB.GetEnum() are used together to modify all other currently active
> enumerations in case the common enumeration object is modified?
>
> Regards,
> Tobi
>
At the moment, there is no support for allocation associated with an
enumeration.
During the _next method, GB.GetEnum() returns a pointer to an array of
four pointers (or four intptr_t, or anything you want that fit). You
have to use that to store your enumeration state, and nothing more.
At the start of the enumeration, the contents of these four pointers is
zero.
An enumerator must handle destruction of other objects. But, for
performance reasons, it cannot ensure that it won't forget any object if
something change in the enumerated objects.
After an object has been removed from the enumerated object, you can use
GB.ListEnum(), GB.NextEnum() and GB.GetEnum() to browse all current
enumerations, and modify their state to take the removal into account.
But this can be a tricky job. Look at gbx_c_collection.c for an example.
At the moment:
- Array enumeration ignores object removal.
- Collection enumeration handles object removal, by checking the state
of each enumeration. If an enumeration points at the object removal, it
is moved forward automatically.
So, as you see, the rule for container classes is: removing an object
during an enumeration has an undefined behaviour, except that the
interpreter won't crash!
Regards,
--
Benoît Minisini
More information about the Devel
mailing list