[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extension module: Why does object get GCed?

pseudoman4

3/13/2006 12:51:00 PM

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

7 Answers

ts

3/13/2006 1:06:00 PM

0

>>>>> "p" == pseudoman4 <pseudoman4@yahoo.com> 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


pseudoman4

3/13/2006 1:29:00 PM

0

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

pseudoman4

3/13/2006 1:33:00 PM

0

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?

ts

3/13/2006 1:38:00 PM

0

>>>>> "p" == pseudoman4 <pseudoman4@yahoo.com> 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


pseudoman4

3/13/2006 2:50:00 PM

0

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?

ts

3/13/2006 3:13:00 PM

0

>>>>> "p" == pseudoman4 <pseudoman4@yahoo.com> 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





pseudoman4

3/13/2006 3:33:00 PM

0

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