From: Eric Hodel Date: 2012-05-18T07:23:51+09:00 Subject: Re: rb_gc_register_address() or rb_gc_mark()? On May 17, 2012, at 12:24, Iņaki Baz Castillo wrote: > 2012/5/17 Iņaki Baz Castillo : >> Ok, after some experiment I'm confused: >> >> - I run GC.startt every 0.1 seconds. >> >> - While my_class instance (which wraps the C struct referencing >> my_string VALUE) remains in the scope (or stored in a global variable) >> my_string is NOT GC'd. I know it because I've stored its object_id and >> used it in ObjectSpace._id2ref(id) and it returns my_string. >> >> So... even if I've seen the usage of rb_gc_mark() in some C >> extensions, in my experiments it does nothing. Any help would be >> appreciated. > > Forget it please! I was storing my_string in a class attribute :( > > Yes, rb_gc_mark() stuf is fully understood. Now I will investigate > what rb_gc_(un)register does, since in my experiments it does NOTHING. Correct. You use rb_gc_mark() to mark ruby objects pointed-to by C structs that are wrapped as ruby objects. If you have struct my_struct { VALUE my_string; } And wrap that with Data_Wrap_Struct, then you must provide a mark function that tells ruby about your string inside your struct (because ruby does not otherwise know it exists). 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.