[英]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.我遇到了一些问题,如果我在 Rails 环境之外运行这个 C 扩展,它可以工作,但是当我在 Rails 中运行时,它会给我一个堆栈转储。
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.这大概是指我在 EV::Counters 中进行的调用评估 function,以及我正在调用的三个实例中存在的“评估”函数。
Strangely valgrind is not giving me any errors.奇怪的是 valgrind 没有给我任何错误。 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_define_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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.