caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Oliver Bandel <oliver@first.in-berlin.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Caml-Exceptions from C-Code and handlingh of Caml-allocations
Date: Fri, 18 Jan 2008 11:12:30 +0000	[thread overview]
Message-ID: <20080118111230.GB9223@annexia.org> (raw)
In-Reply-To: <1200604250.478fc45a531fb@webmail.in-berlin.de>

On Thu, Jan 17, 2008 at 10:10:50PM +0100, Oliver Bandel wrote:
> when throwing Caml-exceptions from C-Code,
> it's clear to me to deallocate C-allocated stuff by myself,
> as I would do in pure C applications also.

Anything allocated using malloc (ie. on the C heap) you must take care
to free.  Most external C libraries that you call will allocate stuff
like that and so you must free it too, in a way that will depend on
the particular C library.  One way to do this is to wrap the pointer
in an OCaml value and attach a finalizer to it.  The finalizer is
called when there are no more OCaml references around, so you can free
the pointer then ... but be careful in case there are still references
held in C code.

Take a look at the function Val_connect here:
http://hg.et.redhat.com/virt/applications/virt-top--devel?f=c29881e5aa70;file=libvirt/libvirt_c.c

> But will throwing Caml-exceptions from C automatically free
> the CAML<something> / caml_alloc allocated stuff?
> (This is what I would expect, but I'm not sure on that.)

Yes - values allocated either from OCaml or in C using caml_alloc /
caml_copy_* etc, are allocated on the OCaml heap and deallocated by
the garbage collector when they are no longer referenced.  In other
words you don't have to worry about them.

Rich.

-- 
Richard Jones
Red Hat


      reply	other threads:[~2008-01-18 11:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-17 21:10 Oliver Bandel
2008-01-18 11:12 ` Richard 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=20080118111230.GB9223@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=oliver@first.in-berlin.de \
    /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).