[Gc] Weak pointers

Boehm, Hans hans_boehm@hp.com
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.

Hans 

> -----Original Message-----
> From: Emmanuel Stapf [ES] [mailto:manus@eiffel.com]
> Sent: Saturday, May 10, 2003 4:24 PM
> To: gc@napali.hpl.hp.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.
> 
> Thanks,
> Manu
> 
> 
> 
> 
> _______________________________________________
> Gc mailing list
> Gc@linux.hpl.hp.com
> http://linux.hpl.hp.com/cgi-bin/mailman/listinfo/gc
>