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=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 39169BBC4 for ; Thu, 19 Feb 2009 18:43:28 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0CAGwpnUnD2MUqgWdsb2JhbACUVQEBFiK+QYQOBg X-IronPort-AV: E=Sophos;i="4.38,236,1233529200"; d="scan'208";a="35477194" Received: from bluelynx4.ukhost4u.com ([195.216.197.42]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 19 Feb 2009 18:43:13 +0100 Received: from [78.105.203.81] (helo=feast.lan) by bluelynx4.ukhost4u.com with esmtpa (Exim 4.69) (envelope-from ) id 1LaCvX-0000Wh-8k; Thu, 19 Feb 2009 17:43:12 +0000 Cc: caml-list@yquem.inria.fr Message-Id: <8C42A4C1-D40D-4B22-AF69-D56A1537D7E0@coherentgraphics.co.uk> From: John Whitington To: Basile STARYNKEVITCH In-Reply-To: <499D9843.9070007@starynkevitch.net> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v930.3) Subject: Re: [Caml-list] Debugging a C / Ocaml interface problem Date: Thu, 19 Feb 2009 17:43:08 +0000 References: <15903958-833F-4698-94AB-DF517CBBCEF2@coherentgraphics.co.uk> <499D9843.9070007@starynkevitch.net> X-Mailer: Apple Mail (2.930.3) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bluelynx4.ukhost4u.com X-AntiAbuse: Original Domain - yquem.inria.fr X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - coherentgraphics.co.uk X-Source: X-Source-Args: X-Source-Dir: X-Spam: no; 0.00; ocaml:01 basile:01 basile:01 val:01 ocaml:01 wikipedia:01 wiki:01 interfacing:01 pointers:01 camlparam:01 val:01 garbage:01 garbage:01 char:01 char:01 Hi Basile, On 19 Feb 2009, at 17:34, Basile STARYNKEVITCH wrote: > John Whitington wrote: >> >> >> int fromFile(char* filename) >> { >> return(Int_val(caml_callback(*caml_named_value("fromFile"), >> caml_copy_string(filename)))); >> } > > You need to follow Ocaml strong garbage collection related rules for > coding in C. > Are you aware of what a garbage collector is for precisely, how a > copying GC works, what exactly are local and global roots? > Reading a few paragraphs could help a lot to get a big picture. > Please read at least http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) > Then read again chapter 18 [interfacing C with Ocaml] of the manual http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html Thanks for the pointers. > The major rule is that every allocated value should be an Ocaml root. Right. I'd misunderstood the rule's use of the phrase "Local variables of type value must be...", and assumed that intermediate values of type 'value' which are not written to C variables didn't require this treatment. Perhaps explicitly mentioning this in the documentation would help others. > Try coding instead // untested! > > int fromFile (char* filename) > { > CAMLparam0; > CAMLlocal3(fromfil_v, filnam_v, res_v); > fromfil_v = *caml_named_value("fromFile"); > filnam_v = caml_copy_string(filename); > res_v = caml_callback(fromfil_v, filnam_v); > CAMLreturnT(int, Int_val(res_v); > } With Thanks, -- John Whitington Coherent Graphics Ltd http://www.coherentpdf.com/