From: Tim Hunter Date: 2008-09-20T02:49:57+09:00 Subject: Re: IRB + C Extension = Memory Loss Ryan Bates wrote: > Thanks! That makes sense. Calling rb_gc_mark(*hello->greeting) in the > hello_mark method works. > > On a related note. What if I need to re-assign greeting to a new > string at some point in the future after hello_mark has been called. > Should I call rb_gc_mark upon assignment (outside of hello_mark)? > > Regards, > > Ryan Let's back up a bit. I assume you're storing the VALUE passed into hello_init in hello->greeting: static VALUE hello_init(VALUE obj, VALUE greeting) { HELLO(obj)->greeting = greeting; return Qnil; } The VALUE represents the String object, which is what Ruby is interested in. In that case, you should call rb_gc_mark(hello->greeting) so Ruby can mark the String object, not the C string embedded in the object. Regarding your new question, when you want to assign a new greeting String object, just store the new VALUE in hello->greeting. The old greeting String won't get marked during the next GC sweep, so GC will know that it's available for collection. (I hope this makes sense. It's so hard to make sense on Friday afternoon :-) -- Posted via http://www.ruby-forum.com/.