Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个定义类的 ruby 扩展。如果我Data_Wrap_Struct()用来实现我的回调rb_define_alloc_func(),我是否需要手动标记和释放实例变量?或者这仍然为我处理?
Data_Wrap_Struct()
rb_define_alloc_func()
Ruby 的 GC 将收集在 Ruby 对象的实例变量中引用的任何 Ruby 对象。您不必也不应该自己释放 Ruby 实例变量(即在您的扩展中使用rb_iv_set()/访问的任何对象)。rb_iv_get()
rb_iv_set()
rb_iv_get()
但是,如果包装的C 结构引用 Ruby 对象,那么您必须在mark传递给的回调中标记这些对象Data_Wrap_Struct()。
mark
free(并且您将始终必须释放底层结构,并在回调中执行任何其他可能需要的清理工作,例如关闭文件、套接字等。)
free