Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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:35)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: [Caml-list] Debugging a C / Ocaml interface problem
Hello All

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
Then read again chapter 18 [interfacing C with Ocaml] of the manual

The major rule is that every allocated value should be an Ocaml root.

Try coding instead // untested!

int fromFile (char* filename)
  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);

Hope this helps.

Don't forget that Ocaml (minor) garbage collector is a copying 
generational garbage collector, and read enough material to understand 
what that is meaning.


email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***