From: Paul Brannan Date: 2002-10-22T23:04:28+09:00 Subject: Re: rb_gc_register_address problem On Tue, Oct 22, 2002 at 10:02:50AM +0900, nobu.nokada@softhome.net wrote: > At Tue, 22 Oct 2002 03:54:24 +0900, > Paul Brannan wrote: > > We ran into a problem today with the garbage collector (caused by our > > own bug). We had a function that did something like this: > > std::vector foo; > > void func(int argc, VALUE * argv, VALUE self) { > > foo.push_back(STR2CSTR(argv[0])); > > rb_global_variable(&argv[0]); > > } > > If this "func" will be called from ruby as a method, you don't > need to call rb_global_variable() here. foo is global, so the call is necessary, so that the string object will still be valid after func() returns. > > So my question is: why does rb_gc_register_address() store VALUE*'s > > instead of VALUE's? I suppose that if it stored a VALUE, then it would > > be possible to register an object as a global but not keep a reference > > to that object; this may not be desirable. In our case, though, we just > > wanted the string to remain valid. > > And introduce memory bugs? Just store the VALUE in a local > variable instead. What memory bugs would be introduced? (My goal here isn't to get the interpreter to change, but to understand why it is written the way it is). Paul