# IRB + C Extension = Memory Loss

**URL:** <https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043>\
**Category:** Ruby\
**Created:** [September 19, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043 "2008-09-19T17:56:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [September 19, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043/1 "2008-09-19T17:56:11Z")

</div>

I’m having difficult making a simple C extension. The struct’s memory  
seems to go missing after a while when using IRB. I’m no C expert, so  
I’m probably doing something wrong as far as memory allocation is  
concerned. Here’s the problem and the code:

> <https://gist.github.com/11611>

Notice the greeting goes away after calling “h.greet” a number of  
times? This only seems to happen in IRB and not through a ruby script.  
Any idea why? I’m assuming it has something to do with Ruby’s garbage  
collection, but I don’t know how to solve it.

Thanks,

Ryan

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [September 19, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043/2 "2008-09-19T19:08:42Z")

</div>

Ryan B. wrote:

> I’m having difficult making a simple C extension. The struct’s memory  
> seems to go missing after a while when using IRB. I’m no C expert, so  
> I’m probably doing something wrong as far as memory allocation is  
> concerned. Here’s the problem and the code:
> 
> [11611’s gists · GitHub](http://gist.github.com/11611)
> 
> Notice the greeting goes away after calling “h.greet” a number of  
> times? This only seems to happen in IRB and not through a ruby script.  
> Any idea why? I’m assuming it has something to do with Ruby’s garbage  
> collection, but I don’t know how to solve it.
> 
> Thanks,
> 
> Ryan

GC is collecting the greeting string because it thinks there are no  
references to it. Your hello\_mark function needs to identify the  
greeting string object as “in use”. From the Pickaxe:

“The mark routine will be called by the garbage collector during its  
``mark’’ phase. If your structure references other Ruby objects, then  
your mark function needs to identify these objects using  
rb\_gc\_mark(value).”

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [September 19, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043/3 "2008-09-19T19:21:18Z")

</div>

Thanks! That makes sense. Calling rb\_gc\_mark(\*hello-\>greeting) in the  
hello\_mark method works.

On a related note. What if I need to re-assign greeting to a new  
string at some point in the future after hello\_mark has been called.  
Should I call rb\_gc\_mark upon assignment (outside of hello\_mark)?

Regards,

Ryan

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [September 19, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/irb-c-extension-memory-loss/149043/4 "2008-09-19T19:57:45Z")

</div>

Ryan B. wrote:

> Thanks! That makes sense. Calling rb\_gc\_mark(\*hello-\>greeting) in the  
> hello\_mark method works.
> 
> On a related note. What if I need to re-assign greeting to a new  
> string at some point in the future after hello\_mark has been called.  
> Should I call rb\_gc\_mark upon assignment (outside of hello\_mark)?
> 
> Regards,
> 
> Ryan

Let’s back up a bit. I assume you’re storing the VALUE passed into  
hello\_init in hello-\>greeting:

static VALUE hello\_init(VALUE obj, VALUE greeting)  
{  
HELLO(obj)-\>greeting = greeting;  
return Qnil;  
}

The VALUE represents the String object, which is what Ruby is interested  
in. In that case, you should call rb\_gc\_mark(hello-\>greeting) so Ruby  
can mark the String object, not the C string embedded in the object.

Regarding your new question, when you want to assign a new greeting  
String object, just store the new VALUE in hello-\>greeting. The old  
greeting String won’t get marked during the next GC sweep, so GC will  
know that it’s available for collection.

(I hope this makes sense. It’s so hard to make sense on Friday afternoon  
🙂
