From: ZaCk Date: 2011-07-03T10:16:29+09:00 Subject: Re: Ruby C API:How to prevent a VALUE being collected by the garbage collecter? --001636c927bcf9c60504a71fffed Content-Type: text/plain; charset=ISO-8859-1 Thanks. Finally I use rb_gc_mark to do this. I am embedding ruby, but the program collapsed when calling rb_funcall. I debugged it and found out that if GC occurs during the calling, the arguments would be collected, and the program throwed a segment fault. I tried rb_gc_mark and rb_gc_force_recycle to protect the arguments. It worked. Maybe ruby is not suitable for embedding. Hah... 2011/7/3 Joel VanderWerf > 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. > > --001636c927bcf9c60504a71fffed--