# Protect objects of a C extension from GC

**URL:** <https://www.ruby-forum.com/t/protect-objects-of-a-c-extension-from-gc/110677>\
**Category:** Ruby\
**Created:** [July 29, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/protect-objects-of-a-c-extension-from-gc/110677 "2007-07-29T14:44:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [July 29, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/protect-objects-of-a-c-extension-from-gc/110677/1 "2007-07-29T14:44:27Z")

</div>

Hi,

I’m writing on a video procession system. In this i have a C extension,  
which creates ruby objects that encapsulate images. This objects are  
passed with some ruby code to an other C extension which shall store  
them for later output.

My problem is, that the GC destroys the objects while they are needed in  
the future. I’ve tried to prevent this by calling

rb\_gc\_register\_address (&myobject);

on the object and after i’m done with the object

rb\_gc\_unregister\_address (&myobject);

But that seems to be ignorred.

Any hints about this?

TIA  
detlef

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [July 29, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/protect-objects-of-a-c-extension-from-gc/110677/2 "2007-07-29T17:23:38Z")

</div>

Detlef R. wrote:

> rb\_gc\_register\_address (&myobject);
> 
> on the object and after i’m done with the object
> 
> rb\_gc\_unregister\_address (&myobject);
> 
> But that seems to be ignorred.

Are you creating the objects using the Data\_Make/Wrap/Get\_Struct macros?  
If so, establish a mark routine that GC can call so you can mark your  
objects as being still in use by calling the rb\_gc\_mark function. See  
Chapter 21 in the Pickaxe.
