Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ask for a more efficient way to deallocate memory (full version)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] Ask for a more efficient way to deallocate memory (full version)
On Dec 9, 2007 10:39 PM,  <Fabrice.Pardo@lpn.cnrs.fr> wrote:
> Hello, and sorry for my previous unfinished message.
>
> As the function Unix.opendir returns a value,
> we can expect that Unix.closedir will be automatically
> called when the dir_handle value is no more referenced.

Well, not really: a GC manages memory, not OS resources like file descriptors.
I wouldn't recommend relying on the GC to close those kind of resources.

Write your code like this:

let with_dir fname f =
  let d = Unix.opendir f in
  try let r = f d in Unix.closedir d ; r
  with exn -> Unix.closedir d ; raise exn

once the "f" function returns, you directory handle is closed.

-- 
  Olivier