Running a "final" finaliser
simonmar at microsoft.com
Tue Dec 23 09:27:23 EST 2003
> Assuming the weak pointers solution is the way to go, I've been
> re-aquainting myself with System.Mem.Weak and now I'm now wondering
> what is an appropriate key for each ForeignPtr.
Before we go down that route, I want to be sure that it's actually
necessary to use weak pointers. It sounds like your application has the
- there is a library that can allocate some resources, where
each resource is represented by a ForeignPtr
- a resource needs to be released when it is no longer referenced
- at some point, we would like to free *all* outstanding resources
(either at the end of the program, or when the library is no
If this is the case, I'd do it something like this:
- keep a global list of the pointers still to be released, probably
a doubly-linked list. Lock the whole thing with an MVar. Elements
are Ptrs, not ForeignPtrs.
- the finaliser on each ForeignPtr removes the corresponding Ptr from
- the final cleanup routine explicitly releases all the remaining
Ptrs in the list, holding the MVar lock as it does so to avoid
race conditions with finalisers.
Weak pointers aren't required, AFAICT.
More information about the Glasgow-haskell-users