From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 4D699BC84 for ; Tue, 22 Mar 2005 17:19:44 +0100 (CET) Received: from shiva.jussieu.fr (shiva.jussieu.fr [134.157.0.129]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j2MGJiXh028755 for ; Tue, 22 Mar 2005 17:19:44 +0100 Received: from localhost (akasha.ijm.jussieu.fr [134.157.173.57]) by shiva.jussieu.fr (8.12.11/jtpda-5.4) with ESMTP id j2MGJcKj097268 ; Tue, 22 Mar 2005 17:19:38 +0100 (CET) X-Ids: 166 Date: Tue, 22 Mar 2005 17:19:37 +0100 (CET) Message-Id: <20050322.171937.30179047.andrieu@ijm.jussieu.fr> To: gava@univ-paris12.fr Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Pb "interface C <-> Ocaml" with array of string From: Olivier Andrieu In-Reply-To: <002b01c52ef4$c6de9120$87cb0e50@mshome.net> References: <002b01c52ef4$c6de9120$87cb0e50@mshome.net> X-Mailer: Mew version 4.1 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-1.7.2 (shiva.jussieu.fr [134.157.0.166]); Tue, 22 Mar 2005 17:19:38 +0100 (CET) X-Miltered: at concorde with ID 424045A0.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at shiva.jussieu.fr with ID 4240459A.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Antivirus: scanned by sophie at shiva.jussieu.fr X-Spam: no; 0.00; caml-list:01 ocaml:01 andrieu:01 andrieu:01 ijm:01 gava:01 threads:01 threads:01 initializing:01 val:01 char:01 pointers:01 pointers:01 argc:01 argv:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: 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