From: Johann Hibschman Date: 2001-12-28T05:34:13+09:00 Subject: [ruby-talk:29483] GC and values in extensions Hi folks, When is it safe to leave a VALUE floating around in an extension module? I want to have C++ classes with VALUEs as members, but I don't know how to protect those from the garbage collector. Is this really a problem, and, if so, what do I have to do about it? In theory, it seems like I would have to somehow tell Ruby that the variable "tmp" contains a ruby object. rb_define_variable will do that for globals, but not for objects that will go away. rb_gc_register_address looks promising, but I'm left wondering if anything bad would happen if I registered a variable which is already accessible through Ruby, or if I registered the same memory location twice. I'm wondering about this, since I'm trying to build a C++ "handle" for Ruby objects, which should somehow tell Ruby that it's holding a reference to the the object upon creation, then tell ruby that it's done with that reference upon destruction, like: void foo(VALUE r_obj) { RBObject cpp_obj(r_obj); // Hold a reference to r_obj <> return; // Destruction of cpp_obj frees ref. } (Well, okay, it doesn't "free" the ref at the end, it just tells the interpreter that it can stop worrying about that address in memory.) If you can give me any pointers, I'd appreciate it. --Johann