Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: David Rajchenbach-Teller <David.Teller@univ-orleans.fr>
To: Gerd Stolpmann <info@gerd-stolpmann.de>
Cc: orbitz@ezabel.com, rossberg@mpi-sws.org, caml-list@inria.fr
Subject: Re: [Caml-list] Scoped Bound Resource Management just for C++?
Date: Wed, 9 Feb 2011 17:52:23 +0100	[thread overview]
Message-ID: <CA021A26-A96C-49C6-A1E0-80F03E8AB53E@univ-orleans.fr> (raw)
In-Reply-To: <1297268092.24058.416.camel@thinkpad>

Actually, in Batteries, we have to go through a number of hoops to be absolutely certain that a stream is flushed before we quit the application.
We have to interact with:
- users manually closing the stream;
- any of the downstream streams being closed for any reason;
- finalization;
- the programmer exiting the program with [exit];
- the user exiting the program with [Ctrl-C];
- ...

Of course, the same issues appear in C++.

Cheers,
 David

On Feb 9, 2011, at 5:14 PM, Gerd Stolpmann wrote:

> Am Mittwoch, den 09.02.2011, 10:15 -0500 schrieb orbitz@ezabel.com:
>> Thanks for the answers everyone.
>> 
>> How does one safely write code in Ocaml that guarantees resources will  
>> be freed?  Guillaume mentioned the with-idiom, but even that doesn't  
>> seem entirely safe.
> 
> You mean C++ is safer in this respect?
> 
> Come on. Fully automatic memory management as in Ocaml is certainly
> safer than any semi-automatic scheme. It will find all memory blocks
> that are not referenced anymore. It's guaranteed. It works even with
> circular structures (this is not a boy GC).
> 
> You would use "with" only for cases where non-memory resources are
> referenced, like file descriptors. And you have to close files in C++,
> too. If you want to be very careful here, you can even set a finaliser
> that emits a warning when you forgot to close a descriptor (but you have
> then to remember whether you closed it), like in
> 
> type managed_fd =
>  { fd : Unix.file_descr;
>    mutable fd_closed : bool
>  }
> 
> (* after opening the file: *)
> let mfd = { fd=fd; fd_closed=false }
> 
> (* Attach the finaliser: *)
> let mfd_fin mfd =
>  if not mfd.fd_closed then
>    prerr_endline "Hey, there is a forgotten file descriptor"
> Gc.finalise mfd_fin mfd
> 
> (* Use mfd as in - ensure you always pass mfd around: *)
> Unix.read mfd.fd ...
> 
> (* When you close: *)
> Unix.close mfd.fd;
> mfd.fd_closed <- true
> 
> I wouldn't recommend to close fd in mfd_fin, because fd might not be a
> simple file, and you can trigger any kind of external activity by
> closing it.
> 
> I've written a number of 24/7 server programs in Ocaml now, and I can
> tell you, resource management is easy. You can usually skip the "search
> for memory leaks" step before deploying to production.



  reply	other threads:[~2011-02-09 16:52 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-08 23:57 orbitz
2011-02-09  0:46 ` Guillaume Yziquel
2011-02-09  0:48 ` Jacques Garrigue
2011-02-09  6:25 ` dmitry grebeniuk
2011-02-09 12:01 ` rossberg
2011-02-09 15:15   ` orbitz
2011-02-09 16:14     ` Gerd Stolpmann
2011-02-09 16:52       ` David Rajchenbach-Teller [this message]
2011-02-09 17:54         ` orbitz
2011-02-09 21:50           ` Jon Harrop
2011-02-10  8:10           ` David Rajchenbach-Teller
2011-02-10 10:39     ` Guillaume Yziquel
2011-02-10 10:59       ` Guillaume Yziquel
2011-02-09 19:11   ` Florian Weimer
2011-02-09 20:10     ` Andreas Rossberg
2011-02-09 20:45       ` Florian Weimer
2011-02-09 21:12         ` Andreas Rossberg
2011-02-10 21:31           ` Florian Weimer
2011-02-09 18:03 ` Jon Harrop
2011-02-09 20:47 ` Norman Hardy
2011-02-09 21:00   ` Gabriel Scherer

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=CA021A26-A96C-49C6-A1E0-80F03E8AB53E@univ-orleans.fr \
    --to=david.teller@univ-orleans.fr \
    --cc=caml-list@inria.fr \
    --cc=info@gerd-stolpmann.de \
    --cc=orbitz@ezabel.com \
    --cc=rossberg@mpi-sws.org \
    /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