Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pb "interface C <-> Ocaml" with array of string
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <andrieu@i...>
Subject: Re: [Caml-list] Pb "interface C <-> Ocaml" with array of string
 Frédéric Gava [Tue, 22 Mar 2005]:
 > 
 > I have a problem with an initialization of a array of strings using a C
 > procedure. I give you the code
 > with some comments. My code works well without threads but when I add  some
 > threads to my code the array is initializing with some strange strings. If
 > someone could help me...

using String_val to pass a caml string to a C function can be unsafe
in several cases. 

Here I guess the C function copies the char* pointers and stores then
somewhere. When they are accessed later (by another function), they
have become invalid because the GC has moved the strings around.  Of
course you can't really know what the function does with the pointers
from the prototype alone, you'd need to see the library source code
(or a good documentation). The solution would be to copy the strings with
strdup() :

 for (i = 0; i < argc; i++) argv[i] = strdup (String_val(Field(arguments, i)));

-- 
   Olivier