From: Eric Hodel Date: 2012-05-18T08:09:59+09:00 Subject: Re: rb_gc_register_address() or rb_gc_mark()? On May 17, 2012, at 15:34, Iņaki Baz Castillo wrote: > 2012/5/18 Eric Hodel : >> To do this you use rb_gc_mark(the_struct->my_string). Without calling rb_gc_mark the garbage collector may reclaim your string which can lead to a crash the next time you try to use it. > > Sure, it's fully understood now, and in fact, without the mark crashes occur. > > Now my other question is about the undocumented > rb_gc_register_address(VALUE object). As per my experience, this is > only valid for VALUE created within ruby code, and not for Ruby > objects that run in Ruby land. For example, in my C extension > XXXXX_init() function I create a Ruby String which I use within my C > code: > > VALUE string_aaa = rb_str_new2("aaa"); > > So I must add: > > rb_gc_register_address(&string_aaa); > > Otherwhise it's GC'd. But if I try to do the same with a previously > existing Ruby object (created in Ruby land or referenced in Ruby land) > then rb_gc_register_address does nothing. For example: > > VALUE MyClass_do_domething(VALUE self) > { > rb_gc_register_address(&self); > return self; > } > > This does NOTHING. The object (self) will be GC'd as usual. rb_gc_register_address is for use with fixed memory addresses like: static VALUE string_aaa; Using it on pointers to the heap or stack is going to result in undefined behavior.