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

I am 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 tag and free instance variables? Or is it still for me?

+6
c ruby ruby-c-extension mark-and-sweep
source share
1 answer

Ruby GC collects any Ruby objects referenced by the instance variables of the Ruby object. You are not required and should not release Ruby instance variables yourself (i.e., any objects accessed using rb_iv_set() / rb_iv_get() in your extension).

However, if the wrapped C structure references Ruby objects, you will need to mark those that are in the mark callback that you pass to Data_Wrap_Struct() .

(And you always need to free the base structure and do any other cleanup you might need, such as closing files, sockets, etc. in your free callback.)

+7
source share

All Articles