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
exception safety / RAII ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-03-07 (00:02)
From: Jon Harrop <jon@j...>
Subject: Re: [Caml-list] exception safety / RAII ?
On Saturday 05 March 2005 18:16, Michael Benfield wrote:
> I'm looking at OCaml coming from sort of a C++ background and I'm
> finding it really exciting.

Yes, it is an excellent language, far better than C++ for virtually all tasks. 
I do suggest that you try to shed most of the C++ ways of thinking though, as 
the vast majority of them do not apply to OCaml. In particular, forget about 
iterators and OOP.

> There's one thing that worries me though. C++ programmers have been
> dealing with issues of exception safety for years - it's a complicated
> problem because coding in the presence of exceptions for all intents
> and purposes means your function could end at any point, so how can you
> make sure resources are deallocated?

This is only a complicated problem if you do not have a garbage collector, 
which OCaml does.

> The C++ solution to this problem 
> is a technique called Resource Acquisition Is Initialization.

This is a poor man's alternative to garbage collection. The principal problem 
is the inability to determine what object owns which resources at any given 
point in the code.

> C++ 
> objects have destructors, which are simply functions that will always
> be called on exit from a scope - including if the exit is caused by an
> exception coming up through your function. You make resource release
> (whether the resource is memory, a socket, whatever) happen in a
> destructor, and then you are set. This is very handy even disregarding
> exceptions.

In OCaml, any internal resources are transparently deallocated by the garbage 
collector so you do not need to worry. In the relatively unlikely event of an 
external resource, you can either explicitly deallocate yourself or you can 
wrap the resource in an OCaml object and set the finaliser of the object to 
deallocate the resource for you. In theory, this could take forever to 
deallocate. In practice, resources are deallocated extremely quickly.

> So I'm just wondering what facilities OCaml has to either implement
> this concept, or other concepts to help with exception safety? The
> OCaml manual says: "Also, finalization can be performed by trapping all
> exceptions, performing the finalization, then raising again the
> exception". This makes me cringe.

This can be done much more elegantly in OCaml than in C++. Do you know how 
this is done in OCaml?

In most cases you probably won't care when a file is closed after writing to 
it, so you can just rely on the garbage collector. In the few cases that you 
do mind, this is neither difficult nor complicated to implement.

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists