English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Weak pointers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1997-04-01 (10:57)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: Weak pointers
> I would like to use weak pointers in my code, and I wrote a little test
> to check its behavior... It does not work. Why? Maybe I did not
> understand them...
> let w = Weak.create 1;;
> let r = ref "hello";;
> Weak.set w 0 (Some !r);;
> print_weak w 0;;
> Gc.full_major ();;
> print_weak w 0;;
> r := "world";;
> print_weak w 0;;

> Some ...
> Some ...
>   I do not understand why the very last print is "Some ..." and not "None".

The problem here is that you use constant string data: these citations
are global references that are never garbage collected, since there
really exists a pointer to them from the global symbol table.

Use strings allocated by make_string (== String.create) instead: you
should observe what you expected to observe.

However, weak pointers are far from being simple to use: you should be
aware of fine grain details about the compiler and the memory manager

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/