From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Gerd Stolpmann <info@gerd-stolpmann.de>
Cc: "Richard W.M. Jones" <rich@annexia.org>, caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Finding "lost" references to OCaml heap values
Date: Tue, 6 Oct 2015 17:17:48 +0200 [thread overview]
Message-ID: <CAPFanBH-ydhhCdXdc7jCQPASOrw0ge=weaTJH1wgPQENLosMVQ@mail.gmail.com> (raw)
In-Reply-To: <1444144145.3571.18.camel@e130.lan.sumadev.de>
[-- Attachment #1: Type: text/plain, Size: 2854 bytes --]
Sorry for the rather naive suggestion, but wouldn't it be possible to
create a weak pointer in the OCaml world, and have the C value refer to
that weak pointer?
On Tue, Oct 6, 2015 at 5:09 PM, Gerd Stolpmann <info@gerd-stolpmann.de>
wrote:
> Am Dienstag, den 06.10.2015, 15:16 +0100 schrieb Richard W.M. Jones:
> > On Tue, Oct 06, 2015 at 02:43:42PM +0100, Richard W.M. Jones wrote:
> > >
> > > I guess I have two questions:
> > >
> > > (1) Is calling Gc.compact () guaranteed to call the finalizer of any
> > > object which is no longer reachable, under all circumstances? Or
> > > would there be some case where it wouldn't be called?
> > >
> > > (2) I have a large mixed OCaml / C program[a] where somehow calling
> > > Gc.compact isn't calling the destructor of a (very) large object.
> > > Manual code inspection has not revealed anything so far --
> > > superficially it appears we are not holding any references to the
> > > object. Is there any method / library / tool that can inspect the
> > > OCaml heap and find references to an object?
> >
> > Always good to explain these things, because the act of explaining it
> > has allowed me to work out why (2) is happening now.
> >
> > The reason is because I was registering a global root from the C heap
> > pointing to the handle, and of course this prevents the handle from
> > being unreferenced.
> >
> > This does, however, raise another question:
> >
> > (3) I want to have a C heap 'value' pointing to an OCaml value, in
> > such a way that if the OCaml value moves around, the C value gets
> > updated to point to the new location. I was using a global root
> > for this purpose, but it seems like global roots really have two
> > purposes:
> >
> > (i) To keep the C value updated if the OCaml value moves.
> >
> > (ii) To act as a global root, preventing the value from being freed.
> >
> > Is there a "weak" global root, that has property (i) but not property
> (ii)?
>
> There is nothing like that in the C API of the OCaml FFI. What you can
> do is to keep only a symbolic reference to the OCaml value from the C
> struct (i.e. if it is the k-th such value just store the number k). The
> mapping from k to the OCaml value can be done with a weak hash table.
> For finalizing this you need to use Gc.finalize.
>
> All in all this is quite complicated. I guess it's only worth it if the
> memory management really needs to be fully-automatic.
>
> Gerd
>
> > Rich.
> >
> > --
> > Richard Jones
> > Red Hat
> >
>
> --
> ------------------------------------------------------------
> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de
> My OCaml site: http://www.camlcity.org
> Contact details: http://www.camlcity.org/contact.html
> Company homepage: http://www.gerd-stolpmann.de
> ------------------------------------------------------------
>
>
[-- Attachment #2: Type: text/html, Size: 3890 bytes --]
next prev parent reply other threads:[~2015-10-06 15:18 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-06 13:43 Richard W.M. Jones
2015-10-06 14:16 ` Richard W.M. Jones
2015-10-06 15:09 ` Gerd Stolpmann
2015-10-06 15:17 ` Gabriel Scherer [this message]
2015-10-06 15:54 ` Richard W.M. Jones
2015-10-06 13:57 Maxime Ransan (BLOOMBERG/ 731 LEX)
2015-10-06 15:53 ` Richard W.M. Jones
2015-10-06 15:57 ` Richard W.M. Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAPFanBH-ydhhCdXdc7jCQPASOrw0ge=weaTJH1wgPQENLosMVQ@mail.gmail.com' \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@inria.fr \
--cc=info@gerd-stolpmann.de \
--cc=rich@annexia.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox