Thanks for all the help with my last set of questions.
I'm writing a function in my C extension where I copy a variable from my object structure to a temporary location. Then, I put a new instance in the new location. However, Ruby keeps garbage collecting the new variable. I fixed this error by disabling GC for awhile, then re-enabling it when I'm done. Is there a nicer way to do that?
Code snippet:
static VALUE
pop_reproduce(VALUE self, VALUE payoffs) {
rb_gc_disable();
Population* p;
Data_Get_Struct(self, Population, p);
VALUE oldArray = p->popArray;
p->popArray = rb_ary_new2(p->size);
/*...do some stuff...*/
rb_gc_enable();
return self;
}
In this code snippet, p->popArray is the one getting GC'd. My mark function only does this:
void pop_mark(VALUE self) {
Population* p;
Data_Get_Struct(self, Population, p);
rb_gc_mark(p->popArray);
}
It's possible that popArray isn't what is getting GC'ed; did I write the mark function wrong? Here's where I register the mark function:
VALUE
pop_new(VALUE class, VALUE size, VALUE tagsize, VALUE strategysize) {
Population* p = ALLOC(Population);
/* do some stuff to the struct*/
VALUE obj = Data_Wrap_Struct(class, pop_mark, free, p);
VALUE argv[3];
argv[0]=size;
argv[1]=tagsize;
argv[2]=strategysize;
rb_obj_call_init(obj, 3, argv);
return obj;
}
Anyway, I realize that's a lot of code to paste... I'm just wondering if there's any way to fix this short of actually disabling the GC.
Thanks,
Austin McDonald