# Retain VALUE from C extension

**URL:** <https://www.ruby-forum.com/t/retain-value-from-c-extension/116871>\
**Category:** Ruby\
**Created:** [October 10, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/retain-value-from-c-extension/116871 "2007-10-10T17:30:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [October 10, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/retain-value-from-c-extension/116871/1 "2007-10-10T17:30:47Z")

</div>

How do I tell ruby to increment it’s garbage collector if I want to  
keep hold of a VALUE in a C extension.

example:

VALUE message;

static VALUE store\_message(VALUE self, VALUE msg) {  
// tell ruby to retain…  
// rb\_retain(msg) ?  
message = msg;  
return Qnil;  
}

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [October 10, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/retain-value-from-c-extension/116871/2 "2007-10-10T19:24:40Z")

</div>

Hi,

In message “Re: retain VALUE from C extension”  
on Thu, 11 Oct 2007 00:29:47 +0900, “Gaspard B.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|How do I tell ruby to increment it’s garbage collector if I want to  
|keep hold of a VALUE in a C extension.

I’m not sure what you mean by ‘increment’ or ‘retain’, but  
rb\_global\_variable() may be the answer. See README.EXT.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [October 10, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/retain-value-from-c-extension/116871/3 "2007-10-10T21:02:51Z")

</div>

Yes, I think this is what I was looking for. As I understand, this  
“declares” the address as needing “global marking” by adding it to  
global\_List.  
/\* mark protected global variables \*/  
for (list = global\_List; list; list = list-\>next) {  
rb\_gc\_mark\_maybe(\*list-\>varptr);  
}

> I’m not sure what you mean by ‘increment’ or ‘retain’, but  
> rb\_global\_variable() may be the answer. See README.EXT.
> 
> ```
> matz.
> 
> ```

Effectivelly, “retain” (objective-C terminology) is not appropriate.  
From what I understand the mark & sweep garbage collecting process is  
quite different from the reference counting strategy I was familiar  
with.

Thanks for the information.

Gaspard
