Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Daniel de Rauglaudre <ddr@p...>
Subject: 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...

$ cat >foo.ml
let print_weak w i =
  begin match Weak.get w i with
    Some _ -> Printf.printf "Some ..."
  | None -> Printf.printf "None"
  end;
  Printf.printf "\n";
  flush stdout
;;

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

Gc.full_major ();;
print_weak w 0;;

$ ocamlc -v
The Objective Caml compiler, version 1.05-2
Standard library directory: ...

$ ocamlc foo.ml

$ ./a.out
Some ...
Some ...
Some ...
Some ...

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

--------------------------------------------------------------------------
 Daniel de RAUGLAUDRE

 Projet Cristal - INRIA Rocquencourt
 Tel: +33 (01) 39 63 53 51
 Email: daniel.de_rauglaudre@inria.fr
 Web: http://pauillac.inria.fr/~ddr/
--------------------------------------------------------------------------