[Gc] Weak pointers
Mon, 12 May 2003 11:13:32 -0700
Look for GC_general_register_disappearing_link in gc.h. This can be used to clear a word when an object becomes unreachable. You also need to ensure that the "weak pointer" itself is not visible to the collector, typically by putting it inside a "pointerfree" ("atomic") object, or by defining "I_HIDE_POINTERS" before including gc.h, and the using HIDE_POINTER/REVEAL_POINTER.
Another alternative is to use a hidden pointer in combination with a finalizer, which clears it. That's a bit tricky to do correctly, since the object may again become reachable between the time the collector determined it to be unreachable and the finalizer is run. You would probably need to keep track of when the weak pointer was last dereferenced, and undo the damage when a dereference occurred since the last GC.
All of these are rather low level facilities. But they can be used to build something cleaner, along the lines you describe.
> -----Original Message-----
> From: Emmanuel Stapf [ES] [mailto:firstname.lastname@example.org]
> Sent: Saturday, May 10, 2003 4:24 PM
> To: email@example.com
> Subject: [Gc] Weak pointers
> Is there a way to implement a weak pointer? That is to say
> something like:
> a_weak_ptr = weak_pointer_of (my_obj);
> Then to access `my_obj' through `a_weak_ptr' by doing something like:
> the_object = access_object_through_weak_ptr (a_weak_ptr);
> If `the_object' is null then it means that `my_obj' was
> collected and it is up to
> the programmer to do the proper thing.
> Gc mailing list