Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
understanding weak
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-10-31 (15:05)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] understanding weak
Daniel Bünzli wrote:
> Le 31 oct. 08 à 03:14, Martin Jambon a écrit :
>> Warren Harris wrote:
>>> I'd like to understand better how ocaml's weak pointers operate. First,
>>> although it doesn't seem to be specified in the documentation, I assume
>>> that weak pointers will *not* be reclaimed (e.g. from a weak hash table)
>>> if the program retains some other reference to the object. I.e. the weak
>>> pointer must be the last remaining pointer to the object for reclamation
>>> to occur.
>> Yes, otherwise the program would crash.
> No since Weak.get returns an option type. As written the documentation
> sounds like the binding could disappear from the array even though the
> program still has references to the value. This could be done, wouldn't
> be usefull, but wouldn't crash the program.

let x = (1, 2);;
let wa = Weak.create 10;;
Weak.set wa 0 (Some x);;
print_int (fst x);;

(fst x) would certainly cause funny effects if x were GC'ed at an
arbitrary time after it has been added to the weak array.

An object can be reclaimed by the GC only if there is no reference to
it. This remains true. Adding an object to a weak array just doesn't
count as a reference.