Hi, I do know this is very commented subject but I don’t understand it
clearly:
I’m coding a Ruby C extension which usess Ruby objects (Proc objects,
class instances…) asynchronously, which means that I need to tell CG
not to try those objects until I use them so they can be GC’d.
Could I know which two C functions should I use?:
- a function for avoiding GC on a given Ruby object (VALUE).
- a function to re-enable GC on a given Ruby object (VALUE).
Thanks a lot.
On Mon, Apr 16, 2012 at 9:31 PM, Iñaki Baz C. [email protected]
wrote:
Hi, I do know this is very commented subject but I don’t understand it clearly:
I’m coding a Ruby C extension which usess Ruby objects (Proc objects,
class instances…) asynchronously, which means that I need to tell CG
not to try those objects until I use them so they can be GC’d.
Could I know which two C functions should I use?:
- a function for avoiding GC on a given Ruby object (VALUE).
- a function to re-enable GC on a given Ruby object (VALUE).
There are at least a couple of ways. One is to decalre these objects
as global variables, but that means they will live for the life of the
program (i.e. no GC until the end)
rb_global_variable
Another way is to register the address with GC. I think this is closer
to what you are asking for:
rb_gc_register_address and rb_gc_unregister_address. Note that these
take a pointer to VALUE, not just a VALUE.
HTH,
Ammar
2012/4/16 Ammar A. [email protected]:
Another way is to register the address with GC. I think this is closer
to what you are asking for:
rb_gc_register_address and rb_gc_unregister_address. Note that these
take a pointer to VALUE, not just a VALUE.
Great, exactly what I was looking for
Thanks a lot.
2012/4/18 Robert K. [email protected]:
Wouldn’t it also work to declare an Array as global variable or
constant and place objects inside until they are no longer needed?
Sure, but that require Ruby operations to insert and remove elements
from an Array, or Hash or whatever.
rb_gc_disable() & rb_gc_enable() works global
but i use an hash for storeing, mark them with rb_global_variable.
and then i add the VALUE objects like this in the hash:
VALUE obj;
rb_hash_aset(global_evthandler,INT2NUM(obj),obj); #protect from GC
rb_hash_delete(global_evthandler,INT2NUM(obj));# unprotect
(as you can see i use the ‘object-ID’ for the hash key)
On Mon, Apr 16, 2012 at 8:54 PM, Ammar A. [email protected]
wrote:
There are at least a couple of ways. One is to decalre these objects
as global variables, but that means they will live for the life of the
program (i.e. no GC until the end)
rb_global_variable
Wouldn’t it also work to declare an Array as global variable or
constant and place objects inside until they are no longer needed?
Kind regards
robert