From: Bob X Date: 2002-08-28T06:22:33+09:00 Subject: Re: Temporary VALUE Needs to Be Protected? William Djaja Tjokroaminata wrote: > Hi, > > I am dealing with this puzzle. We know that we need to protect a global > variable using "rb_global_variable()" which simply calls > "rb_gc_register_address()", so that it will not be swept by Ruby gc. > > Now, regarding the temporary variable such as in > > static VALUE func (VALUE var1, ...) > { > VALUE var2; > .... > var2 = .... > .... > } > > My feeling says that even var1 and var2 should also be protected as in > > static VALUE func (VALUE var1, ...) > { > VALUE var2; > > rb_gc_register_address (&var1); > rb_gc_register_address (&var2); > .... > (some codes) > .... > rb_gc_unregister_address (&var1) > rb_gc_unregister_address (&var2) > return Qnil; > } > > because the "some codes" there may invoke rb_gc(). Is this really a good > practice? > > In my opinion, we have 3 options: > > 1) Make sure that since a VALUE is assigned until it is disposed of no > rb_gc() will occur. I think this is very tricky, because even a function > such as rb_scan_args() seems to be capable of invoking gc as it has some > "rb_ary_new...()" there. > > 2) Always do the pair rb_gc_un/register_address() for each temporary > VALUE. This will make the corresponding C code longer (more typing > :) ). However, this may be an acceptable solution. > > 3) Have a separate Ruby heaps, where we can manually allocate and > deallocate valid Ruby objects without intervention from the gc. This will > probably be the ideal solution, as it is the most efficient in terms of > execution. > > Opinion, anyone? > > Regards, > > Bill If you wanted to protect them couldn't you use $SAFE? This is from a newbie wondering... Bob