* Pb "interface C <-> Ocaml" with array of string
@ 2005-03-22 15:35 Frédéric Gava
2005-03-22 16:19 ` [Caml-list] " Olivier Andrieu
0 siblings, 1 reply; 2+ messages in thread
From: Frédéric Gava @ 2005-03-22 15:35 UTC (permalink / raw)
To: caml-list
Dear Caml-List,
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...
Thanks,
Frédéric Gava
char **argv;
/* "arguments" is the Ocaml Sys.argv array */
value bsmlpub_init(value arguments)
{
int argc,i;
CAMLparam1(arguments);
/* alloc the arguments */
argc = Wosize_val(arguments);
argv = (char**)stat_alloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++) argv[i] = String_val(Field(arguments, i));
argv[i] = NULL;
/* saving them */
/* This function is a C function take from a special library for parallel
computing. So it is impossible to modified this function. This function
gives to all processors the same parameters take from the line command of
the bash. */
bsplib_saveargs(&argc, &argv);
CAMLreturn (Val_unit);
}
/* Them, when a program wants to read the parameters, it calls this
functions, but here
I have an array with bad strings.... */
value bsmlpub_argv(value unit)
{
CAMLparam1(unit);
CAMLreturn (copy_string_array(argv));
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Pb "interface C <-> Ocaml" with array of string
2005-03-22 15:35 Pb "interface C <-> Ocaml" with array of string Frédéric Gava
@ 2005-03-22 16:19 ` Olivier Andrieu
0 siblings, 0 replies; 2+ messages in thread
From: Olivier Andrieu @ 2005-03-22 16:19 UTC (permalink / raw)
To: gava; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-03-22 16:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-22 15:35 Pb "interface C <-> Ocaml" with array of string Frédéric Gava
2005-03-22 16:19 ` [Caml-list] " Olivier Andrieu
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).