Ruby 扩展代码中的段错误
我正在用 C/C++ 编写一个小型 Ruby 扩展,使 boost::dynamic_bitfield 在 Ruby 中可用。我的代码确实可以完美编译,但是当加载扩展并尝试实例化该类时,我遇到了段错误。
我无法正确利用 gdb 来查找错误或错误发生的位置。我想我将问题范围缩小到了 Init_bitfield
或 bf_new
/bf_init
。
完整源码:http://pastebin.com/qLkMGYqq
static VALUE bf_new(VALUE self, VALUE size)
{
VALUE argv[1];
Check_Type(size, T_FIXNUM);
BitField *bf = BitFieldNew(NUM2INT(size));
VALUE tdata = Data_Wrap_Struct(self, 0, free, bf);
argv[0] = size;
rb_obj_call_init(tdata, 1, argv);
return tdata;
}
BitField定义如下:
typedef struct _bitfield {
boost::dynamic_bitset<> data;
} BitField;
代码主要受此启发文章:http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
I'm writing a small Ruby extension in C/C++ that makes boost::dynamic_bitfield available in Ruby. My code does perfectly compile, but when loading the extension and trying to instantiate the class I am getting a segfault.
I haven't been able to properly utilize gdb to find the error or where it is happening. I think I narrowed the problem down to Init_bitfield
or bf_new
/bf_init
.
Full source: http://pastebin.com/qLkMGYqq
static VALUE bf_new(VALUE self, VALUE size)
{
VALUE argv[1];
Check_Type(size, T_FIXNUM);
BitField *bf = BitFieldNew(NUM2INT(size));
VALUE tdata = Data_Wrap_Struct(self, 0, free, bf);
argv[0] = size;
rb_obj_call_init(tdata, 1, argv);
return tdata;
}
BitField is defined as follows:
typedef struct _bitfield {
boost::dynamic_bitset<> data;
} BitField;
The code is mainly inspired by this article: http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是添加:
到 BitFieldNew(size);初始化结构体和 boost::dynamic_bitset。
The solution was to add:
to BitFieldNew(size); to initialize the struct and boost::dynamic_bitset.