English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Debugging a C / Ocaml interface problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-02-19 (17:43)
From: John Whitington <john@c...>
Subject: Re: [Caml-list] Debugging a C / Ocaml interface problem
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