From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 69A30BB81 for ; Thu, 22 Sep 2005 20:33:22 +0200 (CEST) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.194]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j8MIXLTK029682 for ; Thu, 22 Sep 2005 20:33:21 +0200 Received: by xproxy.gmail.com with SMTP id s8so118575wxc for ; Thu, 22 Sep 2005 11:33:14 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=FQMfTWPs9TkmiQGAdWXQh49PH2gAgK2NEUfOH50GRQgHBi4Ro9O/r/UbttXkZNONQcas2NNAQiIE1lWH10q+/Wc9Mup7qE8tu4teGDWZlWyl4tA/r+O+Q5+zVjiV2yMjaADY2EJ4ZxSrlmHXCCu6fgk/VlCyEdKIuzHuLxi9ulc= Received: by 10.70.21.19 with SMTP id 19mr417191wxu; Thu, 22 Sep 2005 11:33:14 -0700 (PDT) Received: by 10.70.51.20 with HTTP; Thu, 22 Sep 2005 11:33:14 -0700 (PDT) Message-ID: <4a708d205092211334a58abfa@mail.gmail.com> Date: Thu, 22 Sep 2005 20:33:14 +0200 From: Lukasz Stafiniak Reply-To: Lukasz Stafiniak To: Dominik Brugger Subject: Re: [Caml-list] Re: Ocaml C interface - Usage of custom blocks Cc: caml-list@yquem.inria.fr, Dominik Brugger In-Reply-To: <87y85pgxhh.fsf@gmx.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <87y85pgxhh.fsf@gmx.de> X-Miltered: at nez-perce with ID 4332F8F1.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; lukasz:01 caml-list:01 ocaml:01 dominik:01 ocaml:01 malloc:01 malloc:01 pointers:01 heap:01 heap:01 decremented:01 lukasz:01 structures:01 explicitly:01 explicitly:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 2005/9/22, Dominik Brugger : > Is there a "best practice" for returning C data to OCaml > which was allocated by malloc? > My bet is on custom blocks. > But section 18.2.3 of the manual points out, that it is potentially > dangerous to free C data, as it might be reclaimed by the OCaml GC. No. It is dangerous to free C data, because you might still use them on the OCaml side. GC will not reclaim "malloc pointers". > So what happens if the data is never explicitly freed? Does the OCaml hea= p > space grow until there is no more memory available to the C part of the > OCaml program? The data (allocated by malloc) is not on OCaml heap. You have memory leak on the C side. > > In my opinion the only way to avoid these problems is the usage of > OCaml custom blocks. My too. The difficulty comes, when the data is also refered by other C data structures. I've solved it by reference counters, decremented explicitly on C side and by finalisation on OCaml side. Lukasz