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
The solution was to add:
new(bf) BitField();
to BitFieldNew(size); to initialize the struct and boost::dynamic_bitset.