caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Whitington <john@coherentgraphics.co.uk>
To: Basile STARYNKEVITCH <basile@starynkevitch.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Debugging a C / Ocaml interface problem
Date: Thu, 19 Feb 2009 17:43:08 +0000	[thread overview]
Message-ID: <8C42A4C1-D40D-4B22-AF69-D56A1537D7E0@coherentgraphics.co.uk> (raw)
In-Reply-To: <499D9843.9070007@starynkevitch.net>

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/



      reply	other threads:[~2009-02-19 17:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-19 17:18 John Whitington
2009-02-19 17:34 ` [Caml-list] " Basile STARYNKEVITCH
2009-02-19 17:43   ` John Whitington [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=8C42A4C1-D40D-4B22-AF69-D56A1537D7E0@coherentgraphics.co.uk \
    --to=john@coherentgraphics.co.uk \
    --cc=basile@starynkevitch.net \
    --cc=caml-list@yquem.inria.fr \
    /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).