From: Tobias Peters Date: 2003-04-16T21:31:40+09:00 Subject: Re: tutorial on embedding ruby (review) On 15 Apr 2003, Steve Hart wrote: > OK Here is an example > > main() > { > ruby_init(); > etc > > test() > rb_gc(); > } > > void test() > { > VALUE v = create me a ruby obj i dont care how > } > > So what happens to v? v is a C variable on the stack and is of course removed when test() returns. I think you rather wanted to ask: "What happens to the ruby object pointed to by v?" Basically, 2 things happen: 1) When test returns, this object becomes _inaccessible_ for your code, since there is no VALUE left that points to it. 2) The rb_gc() call will most likely delete the object (but no 100% guarantee for that) (this uncertainty is caused by ruby's GC algorithm, not by uncertainty of my mind) You should now answer the question: Why would you want the object to stay in memory after you lost any means of accessing it? > > or > > main() > { > > VALUE v; > > ruby_init(); > etc > > test(&v) > rb_gc(); > > do some stuff that uses v > } > > void test( > VALUE *v) > { > *v = create me a ruby obj i dont care how > } After looking at ruby_init() and Init_stack(), I can tell that you've really found a problem here. But if you rewrite it as > main() > { > ruby_init(); do_stuff() > } do_stuff() > { > VALUE v; > > etc > > test(&v) > rb_gc(); > > do some stuff that uses v > } it will work fine and not delete the ruby object as long as v is on the stack T