From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id CDC537ED26 for ; Tue, 29 May 2012 20:59:13 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgsCABscxU/RVdQ2kGdsb2JhbABEo1GRXQgiAQEBAQkJDQcUBCOCGAEBBBICLAEbHQEDDAYFBAEGOyIBEQEFARwGNYdaAQMLC5pVCQOMK4JwhGoKGScNV4hxAQUMinyFLAOVF44VPYQB X-IronPort-AV: E=Sophos;i="4.75,678,1330902000"; d="scan'208";a="160450217" Received: from mail-vb0-f54.google.com ([209.85.212.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 29 May 2012 20:59:13 +0200 Received: by vbmv11 with SMTP id v11so4566338vbm.27 for ; Tue, 29 May 2012 11:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=GacNa83eNL3PzPG/PwirRI0Cx3BUWt9On+RD/YCFSiU=; b=kE6MY/t1rf+HfEnMA7nyobqXmBmwWJV4eZUxkAEqFeJfVGsr20OUCVaovW+j29wGa1 5d6Ab0KYs2fnKIQ+S0CcpbNUtYHCCAPGF9UjIZHlHAxTHbmOFi3GOsS/bktntf5YfJ24 mXGXgH3i6QvgxUvc2TEJpCv2vknL/GUEd4lOtvzELtbvtYzSQOSM4sweonNcHGeaLi7w bSrTmVL4RIkcxY1psmlP71ivY66nTD6M3Im9wAB6D0Apbf3N+vFMZACKw6XVzY5D64BE +Fx1S56qIS0BoIlou8eGWhOEv9jJPTJP0rsrlymwqT95LO6PveCsBwhyYP2lINhmba47 cmTw== Received: by 10.220.209.72 with SMTP id gf8mr14070166vcb.72.1338317951487; Tue, 29 May 2012 11:59:11 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.94.116 with HTTP; Tue, 29 May 2012 11:58:51 -0700 (PDT) In-Reply-To: <4FC517DE.7090705@etorok.net> References: <1337601452.19263.0@samsung> <87fwajtciw.fsf@frosties.localnet> <1338295564.17140.33.camel@thinkpad> <20120529141303.GA4477@siouxsie> <4FC517DE.7090705@etorok.net> From: Philippe Veber Date: Tue, 29 May 2012 20:58:51 +0200 Message-ID: To: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=bcaec54ee4d0184fb504c1316eb8 X-Validation-by: philippe.veber@gmail.com Subject: Re: AW: [Caml-list] Channels not closed on gc? --bcaec54ee4d0184fb504c1316eb8 Content-Type: text/plain; charset=ISO-8859-1 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. --bcaec54ee4d0184fb504c1316eb8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

To make sur= e 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 t= he 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 funct= ions (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 fu= nctions 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/B= atFile.html).

They are indeed very useful !
Cheers,
ph.
--bcaec54ee4d0184fb504c1316eb8--