caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rich@annexia.org>
To: "Neuhaeusser, Martin" <martin.neuhaeusser@siemens.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] The OCaml garbage collector, finalisers, and the right way of disposing native pointers in C bindings
Date: Tue, 12 Jan 2016 13:06:09 +0000	[thread overview]
Message-ID: <20160112130609.GA27249@annexia.org> (raw)
In-Reply-To: <965631B03C670145ABB9F693E51622530D0D763B@DENBGAT9EK5MSX.ww902.siemens.net>

The other replies have covered some of the problems.  You may also be
interested in example code, and we've got lots :-)  Most of the bugs
have now been fixed, after several iterations.  Here are some things
to get you started.

(1) Simple example of a finalizer attached to a custom block:

https://github.com/libguestfs/libguestfs/blob/master/mllib/progress-c.c
https://github.com/libguestfs/libguestfs/blob/master/mllib/progress.ml
https://github.com/libguestfs/libguestfs/blob/master/mllib/progress.mli

(2) A more complex example using generational roots to deal with
callbacks from OCaml back to C:

https://github.com/libguestfs/libguestfs/blob/master/ocaml/guestfs-c.c

This one had a major bug, when we discovered that the root caused the
handle to be always reachable, so the finalizer was not called until
the program exited (the fact that we also have a #close method, which
we always called, made this less obvious than you might think at
first).  That was fixed in:

https://github.com/libguestfs/libguestfs/commit/8bbc5e73cb5b56b5cfbe979ac0e1c14d1701a0d8

(3) A tricky binding to libxml2.

Because libxml2 has objects containing pointers to other objects (at
the C level) we need to shadow these with OCaml structs, to ensure
that an OCaml object doesn't become unreachable when it is still
pointed to from another object.

https://github.com/libguestfs/libguestfs/blob/master/v2v/xml-c.c
https://github.com/libguestfs/libguestfs/blob/master/v2v/xml.ml
https://github.com/libguestfs/libguestfs/blob/master/v2v/xml.mli

If you look at the history of these files, you'll see we discovered
and fixed major bugs, eg this one concerned with the order in which we
freed objects:

https://github.com/libguestfs/libguestfs/commit/3888582da89c757d0740d11c3a62433d748c85aa

Note that (3) is a counter-example to the idea that you should use
custom blocks.  Custom block finalizers in OCaml have no ordering
guarantee - if you need an ordering guarantee you must use an OCaml
finalizer.

You can probably see this stuff gets complicated quickly.  Thank
goodness for valgrind!

Rich.

      parent reply	other threads:[~2016-01-12 13:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-12  9:12 Neuhaeusser, Martin
2016-01-12  9:37 ` Kim Nguyễn
2016-01-12 10:33 ` Gerd Stolpmann
2016-01-12 13:06 ` Richard W.M. Jones [this message]

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=20160112130609.GA27249@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=martin.neuhaeusser@siemens.com \
    /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;
as well as URLs for NNTP newsgroup(s).