# Ruby Extension Code in C, GC Question

**URL:** <https://www.ruby-forum.com/t/ruby-extension-code-in-c-gc-question/130753>\
**Category:** Ruby\
**Created:** [March 3, 2008, 3:20am UTC](https://www.ruby-forum.com/t/ruby-extension-code-in-c-gc-question/130753 "2008-03-03T03:20:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pie\_Squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@Pie\_Squared](https://www.ruby-forum.com/u/Pie_Squared)\
**Post date:** [March 3, 2008, 3:20am UTC](https://www.ruby-forum.com/t/ruby-extension-code-in-c-gc-question/130753/1 "2008-03-03T03:20:53Z")

</div>

I’ve read this article: [http://fixnum.org/ruby\_c.html](http://fixnum.org/ruby_c.html)

It explains how to write a ruby extension in C, and explains how to  
make the GC work with the new types you’ve defined.

However, it leaves me with one question:

What happens if you create a new object inside the C code, and this  
triggers a garbage collection?  
When you create new objects in the C code, are they automatically  
added to the ‘current’ scope, so that the GC sees them? If so, then  
how does it know when to remove them from the scope, and if not, then  
what would happen if a GC cycle was triggered during C code execution?

Thanks  
PieSquared

---

<div class="post-metadata">

**Author:** ![Pie\_Squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@Pie\_Squared](https://www.ruby-forum.com/u/Pie_Squared)\
**Post date:** [March 3, 2008, 3:56am UTC](https://www.ruby-forum.com/t/ruby-extension-code-in-c-gc-question/130753/2 "2008-03-03T03:56:08Z")

</div>

Hi,

In message “Re: Ruby Extension Code in C, GC Question”  
on Mon, 3 Mar 2008 11:19:56 +0900, Pie Squared  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|What happens if you create a new object inside the C code, and this  
|triggers a garbage collection?  
|When you create new objects in the C code, are they automatically  
|added to the ‘current’ scope, so that the GC sees them? If so, then  
|how does it know when to remove them from the scope, and if not, then  
|what would happen if a GC cycle was triggered during C code execution?

The object will be marked by Ruby’s conservative garbage collector,  
which marks objects seemed to be referenced from system stack.

> **[Garbage collection (computer science)](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Precise_vs._conservative_and_internal_pointers)**
>
> In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim memory which was allocated by the program, but is no longer referenced; such memory is called garbage. Garbage collection was invented by American computer scientist John McCarthy around 1959 to simplify manual memory management in Lisp.
> Garbage collection relieves the programmer from doing manual memory management, where the programmer specifies what objects to de-...

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![Pie\_Squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@Pie\_Squared](https://www.ruby-forum.com/u/Pie_Squared)\
**Post date:** [March 3, 2008, 4:12am UTC](https://www.ruby-forum.com/t/ruby-extension-code-in-c-gc-question/130753/3 "2008-03-03T04:12:32Z")

</div>

On Mar 2, 9:54 pm, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> |what would happen if a GC cycle was triggered during C code execution?
> 
> The object will be marked by Ruby’s conservative garbage collector,  
> which marks objects seemed to be referenced from system stack.
> 
> [Garbage collection (computer science) - Wikipedia](http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Pr)…
> 
> ```
> matz.
> 
> ```

Oh! I had missed/misunderstood “conservative” in descriptions of  
Ruby’s GC…

Well, thanks! (And sorry for asking that question when I _should’ve_  
been able to figure it out myself. 🙂 )
