Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Garbage collection and a reference counting library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <damien.doligez@i...>
Subject: Re: [Caml-list] Garbage collection and a reference counting library
On Thursday, December 11, 2003, at 03:36 PM, Richard Jones wrote:

> I wrote an interface to allow OCaml to call Perl code and libraries.
> This interface doesn't pay much attention to collecting memory
> correctly.  In particular when a Perl SV (scalar value) is allocated
> and passed to us, it will have a Perl internal link count of, say, 1,
> and we never decrement that link count, so Perl will never free it.
[...]
> I imagine that what I should do is to add a finalization function, so
> that when 'rv' is garbage collected, I should decrement the Perl link
> count, thus forcing Perl to free the associated SV.

Yes, this is the canonical example of using finalized blocks.

> Is this safe though?  Can OCaml copy this object, and collect one of
> them, thus causing the SV to be deallocated early?  Do I need another
> layer of indirection between 'rv' and the SV?

Of course it is safe.  It is guaranteed that each allocation of a
finalized block corresponds to at most one call of its finalization
function.  Otherwise, finalization would be much less useful.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners