Re: Weak pointers

From: Pierre Weis (Pierre.Weis@inria.fr)
Date: Tue Apr 01 1997 - 12:57:27 MET DST


From: Pierre Weis <Pierre.Weis@inria.fr>
Message-Id: <199704011057.MAA06827@pauillac.inria.fr>
Subject: Re: Weak pointers
In-Reply-To: <199703301713.AA04100@peray.inria.fr> from Daniel de Rauglaudre at "Mar 30, 97 07:13:46 pm"
To: ddr@peray.inria.fr (Daniel de Rauglaudre)
Date: Tue, 1 Apr 1997 12:57:27 +0200 (MET DST)

> 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/



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:10 MET