From: Lyle Johnson Date: 2001-02-23T07:10:05+09:00 Subject: [ruby-talk:11354] Any way to control order in which objects are GC'd? I fear that I already know the answer, but I'll go ahead and ask. But first, the setup. The problem is that for the Ruby extension module I'm working on, I'm wrapping C++ objects that can have references (pointers) to other C++ objects. To avoid a problem of dangling pointers with circular references, one would typically use this kind of pattern in the C++ class destructor: // Destructor for SomeClass SomeClass::~SomeClass() { // clean up all outstanding pointers to me, because I'm about // to be destroyed! } Ruby, of course, doesn't have the concept of C++ destructors. When the GC detects that an object is no longer in use, it will get "collected". So the problem I'm running into is that when object "Foo" gets garbage collected (and the underlying C++ object "CFoo" is destroyed), there are still other C++ objects out there that have references to "CFoo". As soon as they try to access the recently deceased object, bad things happen. So the question is, is there any way to control the order in which objects get garbage-collected? For the current problem I'm debugging, all of this action is taking place when the Ruby interpreter shuts down. For those familiar with the source code, I'm looking at the function "rb_gc_call_finalizer_at_exit()", which is defined in gc.c. The second loop in this function loops over all the data objects and GC's them in an arbitrary order. Any suggestions are welcome...