Q1) I am wondering about the rb_gc_register_address/rb_gc_unregister_address
function pairs.

The both take a VALUE pointer rather than a VALUE. Does that mean that to
unregister I have to pass it the exact same VALUE pointer, or will another
VALUE pointer pointing to the same object be equivalent. I'm asking because
I might rb_gc_register_address and rb_gc_unregister_address multiple times
for the same VALUE, but only once with each VALUE pointer.

Here's a scenario:

VALUE array = rb_ary_new();  // At some point this array is created
somewhere in ruby

.... a little later ...

// I need to pass a reference of the object out of ruby's reach
// so I 'lock' the object with rb_gc_register_address and keep the arrayRef
// pointer with me somewhere safe
VALUE *arrayRef = ALLOC(VALUE);
*arrayRef = array;
rb_gc_register_address(arrayRef)

..... a little later ...

// I need to pass another reference out and do the exact same thing again
VALUE *arrayRef2 = ALLOC(VALUE);
*arrayRef2 = array;
rb_gc_register_address(arrayRef2)

.... a little later again ...

// The second non-ruby object referencing the ruby object is dead and gone
and I
// unregister using the same pointer as I registered with
rb_gc_unregister_address(arrayRef2);
// I then free the pointer that I allocated, assuming that is my
responsibility
free(arrayRef2);

.... a little later again ....

// I do it again because the first non-ruby object referencing the ruby
object is gone
rb_gc_unregister_address(arrayRef);
free(arrayRef);

// At this point the ruby object should be ready to be garbage collected
unless there
// are still references to it in ruby.


Is this the way to do it?



Q2) Another thing that I am wondering about. Can the GC be invoked in the
middle of my C code or does that only happen when control is in the "guts of
ruby". What prevents the VALUEs I have on the stack from being GC'ed in that
case?


/Thomas