Extension module: Why does object get GCed?

P

pseudoman4

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
 
T

ts

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
 
P

pseudoman4

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

pseudoman4

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?
 
T

ts

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
 
P

pseudoman4

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

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?
 
T

ts

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
 
P

pseudoman4

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top