[Ruby/C] Very little piece of code driving me crazy

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
:slight_smile:

#include "ruby.h"
#include <iostream>

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);
}

路路路

--
Posted via http://www.ruby-forum.com/.

Where does it crash? could you paste the output of the binary execution?

路路路

2011/7/14 Michael Jos茅 <sexymimi64@gmail.com>:

#include "ruby.h"
#include <iostream>

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帽aki Baz Castillo
<ibc@aliax.net>

RSTRING(b)->as.heap.ptr
looks funny ... i nont know if its okay.. use
StringValueCStr(b)
instat, or better use one of them:

const char *rb_class2name(VALUE); // for classes
const char *rb_obj_classname(VALUE); // for objects

but be careful, this functions are not typesafe

路路路

--
Posted via http://www.ruby-forum.com/.

It crashes when trying to access RSTRING(VALUE)->as.heap.ptr.

Here is what Ruby outputs during the crash :

<main>: [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.

--
Posted via http://www.ruby-forum.com/.

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

路路路

--
Posted via http://www.ruby-forum.com/.