# [Haskell-cafe] Equivalent of withForeignPtr for System.Mem.Weak?

Antoine Latter aslatter at gmail.com
Sun Oct 31 23:34:12 EDT 2010

On Sun, Oct 31, 2010 at 10:14 PM, Matthew Steele <mdsteele at alum.mit.edu> wrote:
> I have an object to which I have added one or more finalizers via
> addFinalizer from System.Mem.Weak.  I would like to have a function that
> allows me to make use of the object within a block of IO code, and guarantee
> that the finalizer(s) will not be called during the code block -- sort of
> like withForeignPtr, but for arbitrary objects.  Does such a function exist,
> and if not, how could I write one?
>
> I can imagine writing something like:
>
> \begin{code}
> withObject :: a -> IO b -> IO b
> withObject obj action = do
>  result <- action
>  touchObject obj  -- touchObject :: a -> IO ()
>  return result
> \end{code}
>

CC'ing GHC-users list.

The 'primitive' package has a general purpose 'touch' function: