[Gambas-devel] Free state memory when enumeration BREAKs

Tobias Boege tobias at ...692...
Tue Aug 28 18:51:42 CEST 2012


On Tue, 28 Aug 2012, Beno?t Minisini wrote:
> 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
> 

Oh, it is 4*sizeof(intptr_t). This is different in the docs (which says 16
bytes). Hence my confusion. I need 2 pointers and an int. On 64-bit this
exceeds 16 bytes, but will go into 4*sizeof(intptr_t). OK.

It should altogether not be that hard to implement the accounting of removed
objects in enumerations...
Thanks.

Regards,
Tobi





More information about the Devel mailing list