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
[Caml-list] Weak pointers and Gc.finalise
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Shivkumar Chandrasekaran <shiv@e...>
Subject: Re: [Caml-list] Weak pointers and Gc.finalise

On Apr 1, 2004, at 9:07 AM, Damien Doligez wrote:

>> (Side note: I am trying to avoid Marshaling the Bigarray unless it is 
>> going to be garbage-collected. But if I cannot detect that, then I 
>> have to always Marshal it whenever its entries change, which is very 
>> inefficient.)
> I'm not sure I understand.  The finalisation function is called when
> the value is about to be garbage-collected.  Why can't you just
> marshal it at that point?

This session might explain what I am trying to do, and why I could not 
do it.

-----------start session on Mac OS X 10.3.3---------------

         Objective Caml version 3.07+2

# #load "bigarray.cma";;
# open Bigarray;;
# let fname = "Trashme.dat";;
val fname : string = "Trashme.dat"
# let final bat =
   	let fd = open_out_bin fname in
   	Marshal.to_channel fd bat [];
   	close_out fd;;
val final : 'a -> unit = <fun>
# let wt =
   	let wtt = Weak.create 1 in
   	let ba = Array1.create float64 c_layout 1000 in
   	Gc.finalise final ba;
   	Weak.set wtt 0 (Some ba);
val wt :
   (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array1.t 
Weak.t =
# Weak.check wt 0;;
- : bool = true
# Gc.compact ();;
- : unit = ()
# Weak.check wt 0;;
- : bool = false
# Sys.file_exists fname;;
- : bool = false

---------that's it-----------------------

As you can see my finalisation function on the bigarray tries to 
marshal the data to a file. However, after Gc.compact the bigarray is 
no longer reachable from anywhere in my code, but still, the 
finalisation routine has not been called, as is evident from the 
Sys.file_exists call. (I have also tried putting print_endline "Ha" 
statements inside the finalisation routine to check this.)

I do see that Gc.full_major calls the finalisation calls. One way out 
for me is to check if the data has been marshaled successfully, 
otherwise I just issue a Gc.full_major call myself. However, I don't 
want to exercise the GC unnecessarily.

Any advice? Thanks,


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: