Logo Questions Linux Laravel Mysql Ubuntu Git Menu

(U) Ruby Extensions: rb_gc_mark() and instance variables

I'm writing a ruby extension that defines a class. If I use Data_Wrap_Struct() to implement my callback for rb_define_alloc_func(), do I need to manually mark and free the instance variables? Or is that still handled for me?

like image 700
rampion Avatar asked Jun 09 '09 14:06


1 Answers

Ruby's GC will collect any Ruby objects that are referenced in your Ruby object's instance variables. You don't have to, and should not, free Ruby instance variables yourself (i.e. any objects accessed with rb_iv_set() / rb_iv_get() in your extension).

However, if the wrapped C struct references Ruby objects, then you'll have to mark those in the mark callback you are passing to Data_Wrap_Struct().

(And you will always have to free the underlying struct, and do any other clean-up that may be necessary such as closing files, sockets, etc. in your free callback.)

like image 104
molf Avatar answered Oct 21 '22 20:10