caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Caml-Exceptions from C-Code and handlingh of Caml-allocations
@ 2008-01-17 21:10 Oliver Bandel
  2008-01-18 11:12 ` [Caml-list] " Richard Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Bandel @ 2008-01-17 21:10 UTC (permalink / raw)
  To: caml-list

Hello,

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.

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.)

TIA,
   Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Caml-Exceptions from C-Code and handlingh of Caml-allocations
  2008-01-17 21:10 Caml-Exceptions from C-Code and handlingh of Caml-allocations Oliver Bandel
@ 2008-01-18 11:12 ` Richard Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Jones @ 2008-01-18 11:12 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-01-18 11:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-17 21:10 Caml-Exceptions from C-Code and handlingh of Caml-allocations Oliver Bandel
2008-01-18 11:12 ` [Caml-list] " Richard Jones

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).