caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Debugging a C / Ocaml interface problem
@ 2009-02-19 17:18 John Whitington
  2009-02-19 17:34 ` [Caml-list] " Basile STARYNKEVITCH
  0 siblings, 1 reply; 3+ messages in thread
From: John Whitington @ 2009-02-19 17:18 UTC (permalink / raw)
  To: caml-list

Hi Folks,

I'm interfacing our Ocaml PDF stuff to C (threads passim), so main  
programs in C can use the tools.

Here's a bug which I can't track down. A value somewhere in the (pure)  
caml code is changing from one type constructor to another, breaking  
the code. So some tag in a block is being trampled on?

I've gone through all the rules in the manual, and can't find anything  
wrong.

The minimal broken main C program:

caml_startup(argv);
int hello3 = fromFile("hello.pdf");
removeAttachedFiles(hello3);
char data[] = {1,2,3,4,5,6,7,8,9,0};
setMetadataFromByteArray(hello3, data, 10);


Here are those functions:

int fromFile(char* filename)
{
   return(Int_val(caml_callback(*caml_named_value("fromFile"),  
caml_copy_string(filename))));
}

void removeAttachedFiles(int pdf)
{
   caml_callback(*caml_named_value("removeAttachedFiles"),  
Val_int(pdf));
   return;
}

void setMetadataFromByteArray(int pdf, char* data, int len)
{
   CAMLparam0 ();
   CAMLlocal1 (bytestream);
   bytestream = alloc_bigarray_dims(BIGARRAY_UINT8 |  
BIGARRAY_C_LAYOUT, 1, data, len);
   caml_callback2(*caml_named_value("setMetadataFromByteArray"),  
Val_int(pdf), bytestream);
   CAMLreturn0;
}


And here are the types of the caml functions called by those C  
functions:

val fromFile : string -> int

val removeAttachedFiles : int -> unit

val setMetadataFromByteArray : int -> (int,  
Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> unit

All the functions work ok in other circumstances (i.e not in this  
combination).


The failure occurs inside the Caml function setMetadataFromByteArray -  
Pdf.Indirect 27 is changing to Pdf.Integer 27 (Pdf.Indirect and  
Pdf.Integer are two constructors of the same type).

Can anyone spot anything wrong in the above? Something Bigarray- 
related perhaps?

Any suggestions as to how I might go about further debugging this?

With Thanks,

-- 
John Whitington
Coherent Graphics Ltd
http://www.coherentpdf.com/




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Debugging a C / Ocaml interface problem
  2009-02-19 17:18 Debugging a C / Ocaml interface problem John Whitington
@ 2009-02-19 17:34 ` Basile STARYNKEVITCH
  2009-02-19 17:43   ` John Whitington
  0 siblings, 1 reply; 3+ messages in thread
From: Basile STARYNKEVITCH @ 2009-02-19 17:34 UTC (permalink / raw)
  To: John Whitington; +Cc: caml-list

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 
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

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

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);
}

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.

Regards.


-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
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} ***


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Debugging a C / Ocaml interface problem
  2009-02-19 17:34 ` [Caml-list] " Basile STARYNKEVITCH
@ 2009-02-19 17:43   ` John Whitington
  0 siblings, 0 replies; 3+ messages in thread
From: John Whitington @ 2009-02-19 17:43 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

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/



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-02-19 17:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-19 17:18 Debugging a C / Ocaml interface problem John Whitington
2009-02-19 17:34 ` [Caml-list] " Basile STARYNKEVITCH
2009-02-19 17:43   ` John Whitington

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).