Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2003-12-16 (22:54)
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 Archives:
Bug reports: FAQ:
Beginner's list: