Hi there !
I have a very very little piece of code in C using the libruby, and it
crashes… I’m probably missing something but I just can’t put my finger
on it.
Here’s the code :
(I know, I don’t finalize Ruby ^^’ but I was just recoding something
that doesn’t work in my app to find out if it was really coming from the
app or from the piece of code… apparently it’s from the piece of code
#include “ruby.h”
#include
using namespace std;
int main(void)
{
ruby_init();
VALUE test = Qnil;
test = rb_iv_get(test, “@lolwtf”);
if (test == Qnil)
cout << “It is Qnil indeed” << endl;
VALUE a = rb_funcall(test, rb_intern(“class”), 0);
VALUE b = rb_funcall(a, rb_intern(“to_s”), 0);
cout << "Printing classname: " << endl;
cout << RSTRING(b)->as.heap.ptr << endl;
cout << “It didn’t crash” << endl;
return (0);
}
I’m a little puzzled… for the equivalent in Ruby code works very well
:
test = nil
a = test.class
b = a.to_s
puts b
Oh ! And I’m gonna blow your mind a little more.
“VALUE b” IS a Ruby String object.
If you add : rb_funcall(b, rb_intern(“capitalize”), 0);
… Well it works !
2011/7/14 Michael J.é [email protected]:
test = rb_iv_get(test, “@lolwtf”);
if (test == Qnil)
cout << “It is Qnil indeed” << endl;
VALUE a = rb_funcall(test, rb_intern(“class”), 0);
VALUE b = rb_funcall(a, rb_intern(“to_s”), 0);
cout << "Printing classname: " << endl;
cout << RSTRING(b)->as.heap.ptr << endl;
cout << “It didn’t crash” << endl;
return (0);
}
Where does it crash? could you paste the output of the binary execution?
RSTRING(b)->as.heap.ptr
looks funny … i nont know if its okay… use
StringValueCStr(b)
instat
but if you only what the name use
const char *rb_class2name(VALUE); // for classes
const char *rb_obj_classname(VALUE); // for objects
but be careful, this functions are not typesafe
little point, you can use NIL_P(v) to check if its nil, its an macro
wich makes a little bit shorter
and i does not use the using namespace, its okay if you only have one,
but it can crash if you need more of them
It crashes when trying to access RSTRING(VALUE)->as.heap.ptr.
Here is what Ruby outputs during the crash :
: [BUG] Segmentation fault
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
– control frame ----------
c:0001 p:0000 s:0002 b:0002 l:000d24 d:000d24 TOP
– C level backtrace information
/usr/lib/libruby-1.9.1.so.1.9(rb_vm_bugreport+0x72) [0x5bedf2]
/usr/lib/libruby-1.9.1.so.1.9(+0x4d38f) [0x4b838f]
/usr/lib/libruby-1.9.1.so.1.9(rb_bug+0x3a) [0x4b842a]
/usr/lib/libruby-1.9.1.so.1.9(+0xeda34) [0x558a34]
[0x90740c]
./a.out() [0x8048961]
/lib/libc.so.6(__libc_start_main+0xe7) [0x126ce7]
./a.out() [0x8048771]
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension
libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
I tried using class2name and obj_classname: both of them work fine.