Tobias Peters <tpeters / invalid.uni-oldenburg.de> wrote in message news:<Pine.LNX.4.44.0304140936180.15336-100000 / localhost.localdomain>...
> On 13 Apr 2003, Steve Hart wrote:
> 
> > > http://www.rubygarden.org/ruby?GCAndExtensions
> > I did. It was useful as far as it went. What it did'nt say was how the
> > object created with Data_Wrap_Struct could be registered with the GC.
> 
> Thanks for the critique. I don't want to look it up in "Programming Ruby" 
> right now, but basically, Data_Wrap_Struct takes two functions as 
> argument (besides the Struct in question), and creates a new ruby object.
> 
> The new ruby object is automatically known to the Garbage collector. You 
> don't have to do anything else to "register" it.
> 
> But you can register two functions for your new object that will be called
> from the garbage collector. This reqistration is part of
> Data_Wrap_Struct's action. Pass pointers to these functions as arguments
> of the Data_Wrap_Struct macro.
> 
> These two function arguments take function pointers to be called during
> the mark phase and the sweep phase of the GC, respectively. The latter
> will only be called if the object is finally deleted.
> 
> 
> > Neither did it mention how the interpreter marks the current stack
> > frame when ruby_init()
> 
> I'm no expert here, but I don't think it marks the current stack frame, 
> because there is no need to do this. The GC can simply scan the whole C 
> stack for pointers to ruby objects.
> 
> > is called thus enabling c side ruby objects to
> > be automatically marked by the GC.
> 
> If there are pointers to these Objects on the stack (variables of type 
> VALUE), then they will automatically be marked every time the GC enters a 
> mark phase.
> 
> > Knowing this then raises the
> > question of how to tell the GC that, if the previously created object
> > is retained (struct,heap etc), it should not be reaped
> 
> I don't understand this.
> 
> > Finally, it does not explain how to release a retained object when
> > you're done.
> 
> A ruby object is deleted after a GC mark phase, iff it did not recieve a
> mark during that particular mark phase. You can not explicitly delete an 
> object. That's the GC's responsibility.
> 
>   Tobias

Sorry, should have said it was very useful (as far as it went :-). My
point was that if you wrap an object using Data_Wrap_Struct the object
returned resides on the stack - fine. If the object goes out of scope
then GC deletes it. No?
The only way to prevent GC from deleting it would be to save the
object in some system allocated memory AND tell GC using either
rb_gc_register_address or rb_global_variable. rb_gc_register_address
has a complementary function rb_gc_unregister_address which removes
the object from GC's safe list. rb_global_variable does not.

Anyway, my only point really was that the GCAndExtensions could be
expanded into the tutorial simon is writing. No criticism intended.I
would'nt be using Ruby/Swig if I did'nt think they were both pretty
damm fine pieces of software
Cheers

Cheers