Alternatives to finalization

John Meacham john@repetae.net
Mon, 10 Mar 2003 16:38:24 -0800


another possibility is a withStream type function which explicitly
finalizes.

withStream :: Stream a -> ([a] -> IO b) -> IO b

the idea being the stream is freed when the IO b finishes.

this can easily be built on your explicit 'close stream' version below,
but may not be flexable enough for what people want to do with streams.
        John


On Mon, Mar 10, 2003 at 12:59:00AM +0100, Nick Name wrote:
> As the result of a conversation on haskell-gui, I have tried to
> implement the disallocation of resources when a stream is garbage
> collected.
> 
> To explain myself:
> 
> I have a function
> 
> f :: IO [a]
> 
> which returns a lazy stream after allocating some resource to feed it
> (say installing a callback).
> 
> I wish that the resource could be disallocated when it's no longer used.
> I did the obvious implementation with Weak.addFinalizer; results are
> encouraging but not completely satisfying; the scheme I used is:
> 
> f = do
>     allocateResource
>     l <- makeTheStream
>     addFinalizer l (disallocateResource)
>     return l
> 
> The problem is that if no memory is allocated, no garbage collection
> happens; of course finalization is not guaranteed, as the manual states.
> 
> Another alternative is to make f return an esplicit "close stream"
> action:
> 
> f :: IO ([a],IO ())
> 
> Is anyone willing to explain me other alternatives if there are, or to
> tell me that there aren't?
> 
> Thanks for attention
> 
> Vincenzo
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

-- 
---------------------------------------------------------------------------
John Meacham - California Institute of Technology, Alum. - john@foo.net
---------------------------------------------------------------------------