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-12 (22:55)
From: Stefan Monnier <monnier@i...>
Subject: Re: exception safety / RAII ?
>> > However, provided you don't need to make any guarantees about when the
>> > file is  closed during the running of the program, I still think that
>> > implicitly closing a file (or deallocating an external resource) via the
>> > GC is not  sloppy coding. Indeed, this facility can be very useful and
>> > can eliminate an important class of run-time errors.
>> Can you give examples where it's useful?  I don't consider "not needing to
>> call `close' on line NNNN of my code" to be useful.

> OpenGL display lists from earlier in this thread.

Two problems with this example: it doesn't involve files AFAICT, and it's
not an example in any useful sense.  You'd need to describe the skeleton of
the code to explain why it's useful (rather than merely convenient).

>> Can you describe the "important class of runtime errors" that would be
>> supposedly eliminated?

> In that case, undefined rendering behaviour when a display list is executed
> after it has been incorrectly destroyed.  This could also wreck the OpenGL
> state and break rendering for the rest of the frame or even for the rest of
> the program.

[ Ignoring the fact that it still doesn't seem to involve files ] Are such
errors common (or difficult to diagnose/fix) enough that they deserve the
name "important class of runtime errors"? (genuine question: I know next to
nothing about OpenGL).

>> > In the case of files, yes.  More generally, this can be applied to all
>> > sorts of external resources where that is not true.
>> Might be: there's no way we can generalize.  I'm talking about files here.
>> Finalizers are great, but they shouldn't be used for files.

> Let me try another file-specific example then: Programs which save output
> at  some point during their execution.  From the users point of view,
> there is no  "visible" difference between having the program close the
> file as soon as  writing is complete and leaving it up to the GC to close
> the file a short  time afterwards (and before it completes).  I have many
> such programs.

You're arguing beside my point: I don't care whether there are cases where
using finalizers to close files doesn't hurt.  My point is both that there
are cases where they do hurt (even sometimes for the exact same code where
they didn't hurt in other circumstances), and that the added convenience of
not needing to explicitly close the file is insignificant.

So I'd like to see an example where the lack of a "file-close finalizer"
would make the code harder to write.

>> I'd also argue that they generally shouldn't be used for any "external
>> resource".  Though I wouldn't be surprised if there's a counter example
>> somewhere where I'd agree that finalizers are good solution for the
>> management of some particular kind of external resource in some
>> particular circumstance.
> Even if the external resource is memory?

I've already replied to this and even gave another example (CPU), so please
try something else.