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: Fabrice Pardo <Fabrice.Pardo@L...>
Subject: Re: [Caml-list] Ask for a more efficient way to deallocate memory (full version)
Olivier Andrieu wrote:
>
> 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.
>
>   
Thanks, this solution is clear and compact.

I think that all functions allocating resources out of the OCaml heap
should be documented as it, warning the programmer that
he should not be lazy, relying on automatic deallocation of resources.
The fact that Unix.opendir should be better documented
in that sense is marginal for me. It was only an example
of external C-allocated resource.

When using these kind of external C functions,
OCaml seems then less comfortable to the programmer
than reference counted languages.
The price to pay is not too high as functional code permit to encapsulate
the problem as shown by Olivier.

I have a new question now:
What is the drawback if we keep hidden
unsecure external functions (allocating out of the heap
resources, as Unix.opendir),
only publishing secure functions as "with_dir" ?
Of course, I mean other drawback than changing the API.

-- 
Fabrice