From: nobu.nokada@... Date: 2002-10-22T10:02:50+09:00 Subject: Re: rb_gc_register_address problem Hi, 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]); > } > > This is a bug, because once the function returns, argv[0] is invalid, > and global_List contains an invalid VALUE* that may or may not point to > the VALUE we want to keep global. When the GC tries to mark this > VALUE*, it ends up marking an invalid object. If this "func" will be called from ruby as a method, you don't need to call rb_global_variable() here. > 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. -- Nobu Nakada