Thomas wrote:

> How can I hold a reference to a ruby object outside the interpreter?
> Ie, how can I prevent it from being garbage collected if my (external)
> reference is the only one.
> 
> In .net I can use a System.Runtime.InteropServices.GCHandle if I want
> to hold a reference to a managed object in unmanaged code. Calling
> GCHandle.Alloc(object anObject) pins the objects address so the GC
> will never move it and prevents the object from being gc'ed before the
> handle is release by calling its Free() method.
> 
> What is the equivalent technique in ruby?

There are several ways you can do this:

If the VALUE (which is the C data type that points to a Ruby object) is 
on the stack, so long as it doesn't go out of scope, Ruby will not 
collect the object.  This means that, for objects you create for use 
during a function call, you don't need to do ANYTHING; Ruby protects it 
until your function returns.

Another way to do it is to create a C VALUE variable globally or as part 
of a class.  You can call rb_gc_register_address to register the memory 
space holding the VALUE.  So long as your variable exists, Ruby protects 
the object it points to.  When you are done with the object, call 
rb_gc_unregister_address.

If you don't have a fixed memory location holding a VALUE, you can just 
called rb_global_variable to register the object as an unnamed global, 
and it will NEVER get collected.  This is sort of brutal and can lead to 
memory leaks because the object never goes away.

The last way I know of is if you use Data_Make_Struct or 
Data_Wrap_Struct to wrap your own C data type (a struct or whatever) as 
a Ruby object, and your data type has VALUEs pointing to objects and you 
want them to be treated as "children" to your object.  You must provide 
a "mark" function which marks those VALUEs when your object gets marked 
(making them ineligible for collection).

	Sean O'Dell