我在这个 C 扩展中引用实例的方式有问题吗?
我遇到一些问题,如果我在 Rails 环境之外运行这个 C 扩展,它可以工作,但是当我在 Rails 内部运行时,它会给我一个堆栈转储。
我收到此错误消息:
NoMethodError Exception: undefined method `evaluate' for #<String:0x00000103557db0>
这可能是指我在 EV::Counters 评估函数中进行的调用,以及我正在调用的三个实例中存在的“评估”函数。
奇怪的是 valgrind 没有给我任何错误。但我认为我在引用实例的方式上可能做错了一些基本的事情?
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);
}
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_define_method(Counters, "evaluate", evaluate, 3);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要做的是将实例存储为实例变量,例如:
What I needed to do was to store the instances as instance variables, like: