Extension module: Why does object get GCed?

I try to write an exentension module with containing the following
method:
It wraps a c++ and binds it to ruby global variable.

void BindToRubyVariable(someObject* native, char* variableName) {
VALUE klass = get_ruby_class_object_from_somewhere
VALUE ruby_obj = Data_Wrap_Struct(klass, 0, 0, native);
rb_define_variable(variableName, &ruby_obj);
}

I call it like this:

BindToRubyVariable(some_obj1, “foo”);
BindToRubyVariable(some_obj2, “bar”);

Now the problem is that the first object gets GCed, although it should
still be accessible in ruby via $foo. Or am I totally wrong?
Any ideas why it is like that?
What can one to prevent GC killing my object?

Best regards,
gecki

“p” == pseudoman4 [email protected] writes:

p> void BindToRubyVariable(someObject* native, char* variableName) {
p> VALUE klass = get_ruby_class_object_from_somewhere
p> VALUE ruby_obj = Data_Wrap_Struct(klass, 0, 0, native);
^^^^^^^^^^^^^^
p> rb_define_variable(variableName, &ruby_obj);
^^^^^^^^^

at the end of the function, this address is not valid and probably will
not make reference to the new created object

p> }

Guy Decoux

i know that ruby_obj goes out of scope when the function exit. But that
should affect the ruby object should it?

bad, bad:
i meant:
i know that ruby_obj goes out of scope when the function exit. But that
should NOT affect the ruby object should it?

I tried adding ruby_obj to a stl vector. This doesn’t work and the
object is still freed.

The following code works, but is ulgy as hell (and leaky :slight_smile:

class RefHolder {
public:
VALUE val;
};

void BindToRubyVariable(someObject* native, char* variableName) {

RefHolder* r = new RefHolder();
r->val = Data_Wrap_Struct(klass, 0, 0, native);
rb_define_variable(variableName, &(r->val) );

}

Any better ideas?

“p” == pseudoman4 [email protected] writes:

p> i know that ruby_obj goes out of scope when the function exit. But
that
p> should NOT affect the ruby object should it?

After the function exit anything can be put at this address. This mean
that the GC will not mark the ruby object (the address don’t reference
it
any more) and logically it remove it at the sweep phase.

Guy Decoux

Yeah!

rb_gv_set instead of rb_define_variable works perfectly well!

Lots of thanks to “ts”

ps: what i’m doing is to extend a existing c++ app with scripting
capabilities.
Now it is possible to access wrapped c++ objects with a ruby script.
This allows rapid prototyping and stuff like that

“p” == pseudoman4 [email protected] writes:

p> Any better ideas?

First, what are you trying to do ?

If you want a global variable (i.e. $a, $b, …) why you don’t use
rb_gv_set() ?

Guy Decoux