From: Joel VanderWerf Date: 2011-07-03T04:34:01+09:00 Subject: Re: Ruby C API:How to prevent a VALUE being collected by the garbage collecter? On 07/02/2011 11:35 AM, Kim Burgestrand wrote: > > On Saturday, 2 July 2011 at 18:35, Quintus wrote: > >>> Ruby C API:How to prevent a VALUE being collected by the garbage >>> collecter? Is there any function to call? > If you want it to never be collected, make it a global variable. rb_gc_mark will > only do what you want until the next GC cycle. That's right. Take a look at README.EXT in ruby source, which says: > GC should know about global variables which refer to Ruby's objects, but > are not exported to the Ruby world. You need to protect them by > > void rb_global_variable(VALUE *var) So if you do this: VALUE my_global; ... rb_global_variable(&my_global); Then whatever object is referenced by my_global when GC runs will not be collected. If you just want to protect some VALUEs that are not necessarily pointed to by C variables, you could just keep them in a (ruby) array or other data structure, as long as that array is reachable from some variable known to ruby.