Im writing a Ruby extension and Im using the function Data_wrap_struct.
In order to participate in Ruby's mark-and-sweep garbage collection process, I need to define a routine to free my structure, and a routine to mark any references from my structure to other structures. I pass the classic free function to free the memory but I dont know how to use a mark function.
my structs sound like this
typedef struct
{
  int x;
  int y;
} A;
typedef struct
{
  A collection[10];
  int current;
} B;
I think that I need a mark function to mark the references in collection of struct B.
Someone can show me a example to see how a mark function works?
The mark function is used to mark any Ruby objects that your C structure owns.
typedef struct {
    VALUE ruby_object;
} MyStruct;
void mark(void * p) {
    /* p is the wrapped pointer to the MyStruct instance */
    MyStruct * my_struct = (MyStruct *) p;
    rb_gc_mark(my_struct->ruby_object);
}
If the object owned by your structure isn't marked, the garbage collector could sweep it and your code may end up trying to use a finalized object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With