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] faq? gc'd channels & finalize?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-03-19 (10:49)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] faq? gc'd channels & finalize?
> File handles (channels) are not closed on garbage collection, right
> (my tests indicate they're not)?

This is correct.

> Why is this (just because no
> guarantees can be made on when objects will be finalized)?

That's one reason.  Another reason is that when you have a low-level
file descriptor (Unix.file_descr) and build a channel around it
(Unix.{in,out}_channel_of_descr), it is often the case that you don't
want the file descriptor to be closed when the channel becomes garbage
collected -- your code may still be using the file descriptor.

> The docs
> warn against using finalization routines, so it seems like manually
> doing it (or using a wrapper function, which then has to catch and
> reraise exceptions) is the only way.

I'm not sure which part of the doc you're referring to, but with the
new per-object finalization facility of OCaml 3, I think it is safe
to do
        Gc.finalise close_out my_out_channel
        Gc.finalise close_in my_in_channel

for the channels you'd like to see finalized.

- Xavier Leroy
To unsubscribe, mail  Archives: