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
Preventing values from escaping a context
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-02-10 (21:37)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Preventing values from escaping a context
Rich Neswold <> writes:

> On Wed, Feb 10, 2010 at 2:55 AM, Goswin von Brederlow <>
> wrote:
>     Why not make the context a custom block with finalizer? The finalizer
>     then frees the resources. If the context escapes then it remains alive
>     and the finalizer will not be called any time soon. Only when it is no
>     longer reachable. That would also allow someone to create a context
>     andĀ use it for a number of operations before forgeting it.
> Good point. I'll need to consider this as a possible API direction. I have many
> years of C/C++ programming under my belt, so I tend to want to micromanage the
> resources. :)

As an example I have done this while rewriting Unix.file_descr for

My FD is a custom block containing the int. MyUnix.close will set the
int to -1 to signal the FD is no longer valid. / write /
... throw an exception when the FD is -1. And the finalizer outputs a
warning and closes the FD unless it is -1.

That way I can close FDs when I want them closed and I can not leak an
unclosed FD (although you can still keep it alive forever). I also get a
warning when I forget to close an FD either soon after it dies or at the
end of the program if it was kept alive forever.