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

1

6

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?

Undercoating answered 9/6, 2009 at 14:52 Comment(0)
T
7

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.)

Tracheostomy answered 9/6, 2009 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.