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 (20:52)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] understanding weak
Aleksey Nogin wrote:
> On 31.10.2008 07:57, Martin Jambon wrote:
>> 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.
> Martin,
> You are answering the wrong question - you are answering "could x be
> GCed too early?" - the answer is obviously "no". However, the initial
> question was "could (Some x) be removed from wa too early by GC - before
> x is orphaned?" The answer is "we'd hope not", but the documentation is
> somewhat ambiguous.

So I checked the implementation and it turns out that x is unboxed from
(Some x) before being added to the weak array.

x is really the value that matters and of course not keeping any
reference to (Some x) has no importance.

The weak pointer is x (or a null value), stored as a cell of the weak array.