From: Peter Zotov Date: 2011-02-06T08:34:31+09:00 Subject: Re: C extension: How to check if a VALUE is still alive (not being GC'ed)? On Sun, 6 Feb 2011 08:02:39 +0900 IƱaki Baz Castillo wrote: > Hi, I'm coding an async DNS resolver for EventMachine based on udns (a > C library). > > In the Ruby C extension, when submitting a DNS query I pass the > current object (VALUE pointer) as an argument of the DNS query C > function. > Later on, when the DNS server replies, EventMachine (which watches on > the UDP socket of udns) invokes the C callback function belonging to > such query, and that callback includes the given VALUE as one of its > arguments. So I invoke a Ruby method for such VALUE (rb_funcall). > > The problem is that the VALUE could be GC'ed (or being marked for so) > if the programmer assigns it to "nil" before the callback is executed, > so we get a coredump. > > So when the callback is executed I need to check that the given VALUE > is still alive (not GC'ed neither marked for being GC'ed). How can I > inspect that? (I mean in C). > The easiest way would be to assign this pointer to the resolver object as an instance variable (with rb_iv_set), or perhaps store it inside a hash which maps request ID's to pointers, if you want to be able to use the same resolver for several simultaneous callbacks. -- WBR, Peter Zotov