From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 69469BC6C for ; Fri, 18 Jan 2008 12:12:36 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMYYkEfAXQInh2dsb2JhbACQFgEBAQgKKZx6 X-IronPort-AV: E=Sophos;i="4.25,216,1199660400"; d="scan'208";a="6277294" Received: from concorde.inria.fr ([192.93.2.39]) by mail2-smtp-roc.national.inria.fr with ESMTP; 18 Jan 2008 12:12:36 +0100 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m0IBCRc5011443 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 18 Jan 2008 12:12:36 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CABIYkEdQRFuw/2dsb2JhbACtUA X-IronPort-AV: E=Sophos;i="4.25,216,1199660400"; d="scan'208";a="21470910" Received: from furbychan.cocan.org ([80.68.91.176]) by mail4-smtp-sop.national.inria.fr with ESMTP; 18 Jan 2008 12:12:31 +0100 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1JFp9C-0002lC-QV; Fri, 18 Jan 2008 11:12:30 +0000 Date: Fri, 18 Jan 2008 11:12:30 +0000 To: Oliver Bandel Cc: caml-list@inria.fr Subject: Re: [Caml-list] Caml-Exceptions from C-Code and handlingh of Caml-allocations Message-ID: <20080118111230.GB9223@annexia.org> References: <1200604250.478fc45a531fb@webmail.in-berlin.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1200604250.478fc45a531fb@webmail.in-berlin.de> User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Miltered: at concorde with ID 4790899B.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; 0100,:01 bandel:01 malloc:01 pointer:01 ocaml:01 finalizer:01 finalizer:01 ocaml:01 pointer:01 val:01 alloc:01 alloc:01 deallocated:01 garbage:01 wrote:01 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 / 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