Forum: Ruby Extension module: Why does object get GCed?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-13 14:54
(Received via mailing list)
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
ts (Guest)
on 2006-03-13 15:06
(Received via mailing list)
>>>>> "p" == pseudoman4  <removed_email_address@domain.invalid> 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
unknown (Guest)
on 2006-03-13 15:34
(Received via mailing list)
i know that ruby_obj goes out of scope when the function exit. But that
should affect the ruby object should it?
unknown (Guest)
on 2006-08-03 15:26
(Received via mailing list)
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 (Guest)
on 2006-08-03 15:26
(Received via mailing list)
>>>>> "p" == pseudoman4  <removed_email_address@domain.invalid> 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
unknown (Guest)
on 2006-08-03 15:27
(Received via mailing list)
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 (Guest)
on 2006-08-03 15:27
(Received via mailing list)
>>>>> "p" == pseudoman4  <removed_email_address@domain.invalid> 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
unknown (Guest)
on 2006-08-03 15:28
(Received via mailing list)
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
This topic is locked and can not be replied to.