Is there a weak pointer or references in D?

thedeemon dlang at thedeemon.com
Sat Jan 12 02:58:22 PST 2013


On Saturday, 12 January 2013 at 02:49:40 UTC, Jonathan M Davis 
wrote:

> That doesn't fly with the GC, because then you can have a 
> reference to memory
> that's been freed - though I suppose that it could work if the 
> weak reference
> were set to null when the normal reference was collected.

That does fly with GC in languages that are aware of weak 
pointers. Like OCaml which has a moving GC. From its manual:
"A weak pointer is a value that the garbage collector may erase 
whenever the value is not used any more (through normal pointers) 
by the program. Note that finalisation functions are run after 
the weak pointers are erased.
A weak pointer is said to be full if it points to a value, empty 
if the value was erased by the GC."
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Weak.html

So the runtime is aware of weak pointers and clears them to 
"empty" state when pointed value dies. I don't see yet how it can 
be implemented in D without patching its GC.


More information about the Digitalmars-d-learn mailing list