From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id B2584BC75 for ; Sat, 5 Mar 2005 19:44:26 +0100 (CET) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.173]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j25IiQLg010647 for ; Sat, 5 Mar 2005 19:44:26 +0100 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1D7eGG-0004bD-00; Sat, 05 Mar 2005 19:44:24 +0100 Received: from [80.129.98.140] (helo=gate.lan.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1D7eGF-0002gZ-00; Sat, 05 Mar 2005 19:44:24 +0100 Received: from flakew.lan.gerd-stolpmann.de (flakew [192.168.0.32]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id AA2F4C052; Sat, 5 Mar 2005 19:44:23 +0100 (CET) Subject: Re: [Caml-list] exception safety / RAII ? From: Gerd Stolpmann To: Michael Benfield Cc: caml-list@yquem.inria.fr In-Reply-To: <293072a520e3724a0497e6456a8675be@mac.com> References: <293072a520e3724a0497e6456a8675be@mac.com> Content-Type: text/plain Date: Sat, 05 Mar 2005 19:44:22 +0100 Message-Id: <1110048263.5856.14.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at nez-perce with ID 4229FE0A.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 gerd:01 stolpmann:01 gerd:01 ocaml:01 deallocated:01 deallocated:01 garbage:01 higher-order:01 ocaml:01 descriptors:01 o'caml's:01 o'caml:01 stolpmann:01 viktoriastr:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Am Samstag, den 05.03.2005, 13:16 -0500 schrieb Michael Benfield: > I'm looking at OCaml coming from sort of a C++ background and I'm > finding it really exciting. > > 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 not as complicated as for C++. Memory is deallocated automatically by the garbage collector. The remaining resources are usually handled manually, e.g. for files let f = open_in ... in try ... (* code may raise arbitrary exception *) with any_exception -> close_in f; raise any_exception If that happens frequently, one can also define higher-order functions for that purpose, e.g. let with_in_file f fn = try let r = fn f in close_in f; r with any_exception -> close_in f; raise any_exception and then with_in_file (open_in ...) (fun f -> ...) > The C++ solution to this problem > is a technique called Resource Acquisition Is Initialization. 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. > > 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. Using finalization functions is often a bad idea. You cannot predict when they are called. For example, if one tried to close files by finalization it would be very likely to run out of file descriptors because finalization is deferred to some unknown point in time in the future. The main purpose of finalization is to synchronize O'Caml's memory management with some foreign mechanism, e.g. because one is calling a foreign library. So the simplified answer to your question: There is nothing like RAII, and one does not miss it, because O'Caml has much better concepts. Gerd -- ------------------------------------------------------------ Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de ------------------------------------------------------------