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