[Haskell-cafe] Finalizer registered with System.Mem.Weak.addFinalizer is called surprisingly early

balodja balodja at zlug.asia
Wed Mar 9 22:54:07 CET 2011


Hello,

I use addFinalizer from System.Mem.Weak for performing some actions on
data just before that is reclaimed by GC. Registered in this way
finalizer is called surprisingly early for me, much earlier the data is
tend to be collected.

I've attached source code of simple two-threaded program. Source code
also can be found at [1]. It is pretty simple and should be readable
enough.

Program is intentioned to pass messages through transactional variable
(TVar) for printing by worker thread. Messages consist of formatted to
String Int values from 1 to 1000000. So the output should look like
this:

> 1
> 2
> ...
> 1000000

But running the program gives something like that:

> 1
> 2
> ...
> 283
> finalizing
> Test: thread blocked indefinitely in an STM transaction

As far as I see, finalizer is called too early. If someone could point
me out how to do the right thing, that would be helpful.

Operaton system: Debian Squeeze
Compiler: GHC 6.12.1
Compiling options: none, just "ghc --make Test.hs"

Thanks,
balodja

[1] http://hpaste.org/44648

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.hs
Type: text/x-haskell
Size: 1697 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110310/ec217ad5/attachment.hs>


More information about the Haskell-Cafe mailing list