Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
http://www.coherentpdf.com/