caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Weak pointers and Gc.finalise
@ 2004-04-01  4:48 Shivkumar Chandrasekaran
  2004-04-01 17:07 ` Damien Doligez
  0 siblings, 1 reply; 3+ messages in thread
From: Shivkumar Chandrasekaran @ 2004-04-01  4:48 UTC (permalink / raw)
  To: caml-list

I just noticed that if the only reference to a bigarray is in a Weak.t 
array, and if there is a finalisation function set on the bigarray, 
then that finalisation function is not necessarily called when the 
entry in the Weak.t array is set to None. Rather it is potentially 
called *much* later.

I am assuming that this is the right behaviour.

My question is, can I get the finalisation function to be called when 
the weak pointer is being set to None?

(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.)

Thanks,

--shiv--

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Weak pointers and Gc.finalise
  2004-04-01  4:48 [Caml-list] Weak pointers and Gc.finalise Shivkumar Chandrasekaran
@ 2004-04-01 17:07 ` Damien Doligez
  2004-04-01 19:21   ` Shivkumar Chandrasekaran
  0 siblings, 1 reply; 3+ messages in thread
From: Damien Doligez @ 2004-04-01 17:07 UTC (permalink / raw)
  To: caml-list

On Apr 1, 2004, at 06:48, Shivkumar Chandrasekaran wrote:

> I just noticed that if the only reference to a bigarray is in a Weak.t 
> array, and if there is a finalisation function set on the bigarray, 
> then that finalisation function is not necessarily called when the 
> entry in the Weak.t array is set to None. Rather it is potentially 
> called *much* later.

That's right.  Within a GC cycle, we first handle the weak pointers
that have to be erased, then we handle the finalised values.

> I am assuming that this is the right behaviour.

We have to do it that way because there might be several weak pointers
to the same value, and they have to be all erased before we call
the finalisation function.

> My question is, can I get the finalisation function to be called when 
> the weak pointer is being set to None?

I guess it might be possible to attach functions to weak pointers,
to be called when the weak pointer is erased, but that would be
different from finalisation functions, and it would need a large
amount of work to modify the GC.

> (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?

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Weak pointers and Gc.finalise
  2004-04-01 17:07 ` Damien Doligez
@ 2004-04-01 19:21   ` Shivkumar Chandrasekaran
  0 siblings, 0 replies; 3+ messages in thread
From: Shivkumar Chandrasekaran @ 2004-04-01 19:21 UTC (permalink / raw)
  To: caml-list; +Cc: Damien Doligez


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);
   	wtt;;
val wt :
   (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array1.t 
Weak.t =
   <abstr>
# 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,

--shiv--

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-04-01 19:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-01  4:48 [Caml-list] Weak pointers and Gc.finalise Shivkumar Chandrasekaran
2004-04-01 17:07 ` Damien Doligez
2004-04-01 19:21   ` Shivkumar Chandrasekaran

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).