From: "Iñaki Baz Castillo" Date: 2012-05-18T03:45:48+09:00 Subject: Re: rb_gc_register_address() or rb_gc_mark()? 2012/5/17 Iñaki Baz Castillo : > I would like to learn a bit more about rb_gc_mark(). In my experiments > such a function is never executed if the object is not assigned to a > variable or stored in a hash/array/whatever. But, in case it's stored > somewhere, then the mark() function is executed multiple times by > Ruby. > > Is it the normal behavior? Forget this please, this happens because I've a thread running GC.start every 0.1 seconds. I've understood how "mark" works: When GC runs (Ruby decides when) it checks all the allocated VALUEs and "marks" all the values that are in the current scope or are stored in some attribute/array/hasy. If an object out of the current scope is not referenced it's not marked so, next to the "mark" action, Ruby performs "sweep" action which frees all the non marked objects. Hope this is correct :) Unfortunatelly I still don't know how to translate this stuff to Ruby C extensions. My question is very simple: - MyClass_alloc() function mallocs a C struct. - The C struct contains a "my_string" VALUE field and I assign there i.e. a Ruby String (which does not exist in Ruby land). - Of course MyClass_free() function should free the C struct. But, what about "my_string" VALUE within the C struct? - Let's say that I've an instance of MyClass in a global variable $my_class. - Then I run GC.start. Would it free my_string VALUE? (yes/no) - In case of yes, should then I provide a MyClass_mark() function which does this?: rb_gc_mark(data->my_string) And then, what's the meaning of rb_gc_(un)register_address(&VALUE)? For me it means "this VALUE will never been GC'd even if it's not referenced in Ruby land. Thanks a lot. -- Iñaki Baz Castillo