# Extension module: Why does object get GCed?

**URL:** <https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305>\
**Category:** Ruby\
**Created:** [March 13, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305 "2006-03-13T13:54:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 13, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/1 "2006-03-13T13:54:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 13, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/2 "2006-03-13T14:06:57Z")

</div>

> > > > > “p” == pseudoman4 [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 13, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/3 "2006-03-13T14:34:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/4 "2006-08-03T13:26:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 1:27pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/5 "2006-08-03T13:27:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/6 "2006-08-03T13:26:57Z")

</div>

> > > > > “p” == pseudoman4 [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/7 "2006-08-03T13:28:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 1:27pm UTC](https://www.ruby-forum.com/t/extension-module-why-does-object-get-gced/80305/8 "2006-08-03T13:27:43Z")

</div>

> > > > > “p” == pseudoman4 [removed\_email\_address@domain.invalid](mailto: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
