caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Philippe Veber <philippe.veber@gmail.com>
To: "Török Edwin" <edwin+ml-ocaml@etorok.net>
Cc: caml-list@inria.fr
Subject: Re: AW: [Caml-list] Channels not closed on gc?
Date: Tue, 29 May 2012 20:58:51 +0200	[thread overview]
Message-ID: <CAOOOohQMkXp6CF_y2_MqvWLMQ4TDd0Qc2=RGZLGogo-fBDkdHA@mail.gmail.com> (raw)
In-Reply-To: <4FC517DE.7090705@etorok.net>

[-- Attachment #1: Type: text/plain, Size: 1042 bytes --]

To make sure that filehandles are closed properly I've lately written
> with_file functions that take a filename and a function.
> It then opens the file, invokes the function (in a try block), and closes
> the file (even if the try block threw an exception).
> That way no file handles leak, and the GC is not needed to clean up.
>
> This is in some ways similar to RAII in C++ where destructors
> automatically clean up resources when you leave a scope.
> Maybe there should be a small library that provides these convenience
> functions (other resources can be handled similarly,
> opendir/closedir comes to mind, but there are probably other cases where
> an explicit close-like functionality is needed).
>

Those functions are defined in Batteries, see BatPervasives.with_dispose (
http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatPervasives.html),
BatFile.with_file_in and BatFile.with_file_out (
http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatFile.html
).

They are indeed very useful !
Cheers,
ph.

[-- Attachment #2: Type: text/html, Size: 1446 bytes --]

  reply	other threads:[~2012-05-29 18:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-21 11:23 Lauri Alanko
2012-05-21 11:57 ` AW: " Gerd Stolpmann
2012-05-21 12:53   ` Philippe Wang
2012-05-21 13:31     ` Gerd Stolpmann
2012-05-21 14:18       ` Philippe Wang
2012-05-21 14:48         ` Mehdi Dogguy
2012-05-29 12:08   ` AW: " Goswin von Brederlow
2012-05-29 12:46     ` Gerd Stolpmann
2012-05-29 14:13       ` oliver
2012-05-29 18:39         ` Török Edwin
2012-05-29 18:58           ` Philippe Veber [this message]
2012-05-29 12:49     ` Jérémie Dimino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOOOohQMkXp6CF_y2_MqvWLMQ4TDd0Qc2=RGZLGogo-fBDkdHA@mail.gmail.com' \
    --to=philippe.veber@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=edwin+ml-ocaml@etorok.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).