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 (02:22)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] understanding weak
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.

> My second question relates specifically to my application. I would like
> to have a primary cache of objects, and a secondary index into
> sub-objects referenced from the primary cache. I.e. CacheA references
> objects of type A; objects of type A reference objects of type B; CacheB
> references objects of type B. I would like to guarantee that weak
> references in CacheB are not flushed unless the corresponding reference
> from CacheA is first flushed. I assume will be the case if a non-weak
> reference from A to B is maintained. Can anyone verify?

Weak pointers are useful for data that should be shared by several
independent "users" and discarded when the number of users drops to zero.

I wouldn't call your CacheB a cache if it is a weak array or a weak hash
table. It is a table of shared objects. These objects of type B are
shared by objects of type A and possibly other users of CacheB.