> > This is a big problem because the C++ object that this Data object
> > refers to has in fact been destroyed by this point, and so the Ruby Data
> > object has a "dangling" pointer. The code inside the mark function for
> > this class assumes that the object hasn't been destroyed yet; after all,
> > Ruby thinks it's still alive and so it must be OK. And we all know how
> > ugly things can get when you try to dereference a dangling pointer.
>
> How are these pointers getting freed?  The pointers should never ever be
> freed until the Ruby objects using them are freed.  You can use the C
> destructor function for Ruby objects to do this.
>
> If this isn't possible (i.e. you have to destroy the C++ objects at a
> certain time), then you should contain back pointers to the Ruby objects
> (to unset their pointer) or something similar, to ensure there is never
> a dangling pointer.

These are "borrowed" pointers, which point to items from a container that
"owns" them. One of the container object's member functions, clearItems(),
destroys all of the items (C++ objects) in the container. I think that I can
work around that problem since I know which of the containers' member
functions are destructive in this way and I can take precautions to unset
the corresponding Ruby objects' pointers before the C++ items get clobbered.

I completely agree with you that if I intentionally hang on to a Ruby object
which refers to a dangling pointer, and try to continue to use that object,
that I'm going to get into trouble. No argument there. The weird thing I'm
seeing (as described in the original post) is that, as far as I can tell, my
code isn't holding any references to the Ruby Data object in question. But
as Matt Armstrong pointed out in a separate response, there are some obscure
ways in which additional references to objects can be created. So I can
accept the likelihood that there is in fact a reference to this object,
somewhere in the interpreter, that I don't realize I've created. I'm now
just really really curious about how to track it down ;)