On Sat, Mar 01, 2003 at 12:04:03AM +0900, Friedrich Dominicus wrote:
> >        /* there are many chances than the String referenced by obj
> >           was removed by the GC, and you have a segfault when you try
> >           to access RSTRING(obj)->ptr[0] */
> Ok I understand that now, what do I have to do that it does not
> happen? 
> 
> What's the simplest but too safe way to solve my simple problem. I
> just want the C side access the Ruby side as partly pointed out here. 

Wrap your code inside a function (with the variables declared inside)
and call it _after_ doing ruby_init().

Explanation (probably inaccurate):

Ruby's GC is conservative, meaning that it will look for references to
objects in C's stack. However, the base of the stack is found when you
call ruby_init, so if there were values in a deeper stack-frame they will
be ignored.

The sequence of (pertinent) functions called is
  ruby_init 
  Init_heap
  Init_stack -> gets the start of C's stack by taking the address of an
                argument and saves it for later GC runs

then at some point when marking the GC does
    rb_gc_mark_locations(rb_gc_stack_start, (VALUE*)STACK_END);

that's why it will only mark objects held in stack frames over (less
deep than) the initial one (the one ruby_init was run in).


-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

This is a scsi driver, scraes the shit out of me, therefore I tapdanced
and wrote a unix clone around it (C) by linus
	-- Somewhere in the kernel tree