开发者

Is there something wrong with how I'm referencing my instances in this C extension?

I'm having some issues where when if I run this C extension outside of a Rails environment it works, but when I run inside Rails it gives me a stack dump.

I get this error message:

NoMethodError Exception: undefined method `evaluate' for #<String:0x00000103557db0>

This is presumably referring to the calls I am making within the EV::Counters evaluate function, to the "evaluate" functions that exist in the three instances that I am calling.

Strangely valgrind is not giving me any errors. But I think there is something basic I might be doing wrong with how I reference my instances?

VALUE rFlushInstance, rPairCounterInstance, rStraightInstance;


static VALUE 
evaluate(VALUE self, VALUE val, VALUE suit, VALUE index) 
{
    rb_funcall(rFlushInstance, rb_intern("evaluate"), 3, val, suit, index);
    rb_funcall(rStraightInstance, rb_intern("evaluate"), 2, val, index);
    rb_funcall(rPairCounterInstance, rb_intern("evaluate"), 2, val, index);

    return Qnil;
}

VALUE EV;

void Init_counters() 
{
    EV = rb_define_module("EV");
    VALUE Counters = rb_define_class_under(EV, "Counters", rb_cObject); 
    init_pair_counter();
    init_straight();  
    init_flush();

    VALUE Flush = rb_const_get(EV, rb_intern("Flush"));
    VALUE PairCounter = rb_const_get(EV, rb_intern("PairCounter"));
    VALUE Straight = rb_const_get(EV, rb_intern("Straight"));
    rFlushInstance = rb_class_new_instance(0, NULL, Flush);
    rStraightInstance = rb_class_new_instance(0, NULL, Straight);
    rPairCounterInstance = rb_class_new_instance(0, NULL, PairCounter);

    rb_define_method(Counters, "initialize", initialize_counters, 2);
    rb_de开发者_C百科fine_method(Counters, "evaluate", evaluate, 3);

}


What I needed to do was to store the instances as instance variables, like:

VALUE rPairCounterInstance = rb_class_new_instance(0, NULL, PairCounter);   
rb_ivar_set(self, rb_intern("@pair"), rPairCounterInstance);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜