The above informal specification is fine for simple situations, but matters can get complicated. In particular, it needs to be clear exactly when a key dies, so that any weak pointers that refer to it can be finalized. Suppose, for example, the value of one weak pointer refers to the key of another...does that keep the key alive?
The behaviour is simply this:
deRefWeak
return Nothing
; and (b) run the finalizer.This behaviour depends on what it means for a key to be reachable. Informally, something is reachable if it can be reached by following ordinary pointers from the root set, but not following weak pointers. We define reachability more precisely as follows A heap object is reachable if:
The root set consists of all runnable threads, and all stable pointers
(see Section
Stable Pointers).
NOTE: currently all top-level objects are considered to be reachable,
although we hope to remove this restriction in the future. A
Char
or small Int
will also be constantly reachable, since
the garbage collector replaces heap-resident Char
s and small
Int
s with pointers to static copies.
Notice that a pointer to the key from its associated value or finalizer does not make the key reachable. However, if the key is reachable some other way, then the value and the finalizer are reachable, and so, therefore, are any other keys they refer to directly or indirectly.