From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 24E81BC57 for ; Tue, 2 Mar 2010 00:01:51 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhEBADbWi0vUGyoGkWdsb2JhbACRG4lsFQEBAQEJCwoHEwMfiBuldoYsLYhPhHsE X-IronPort-AV: E=Sophos;i="4.49,563,1262559600"; d="scan'208";a="53835875" Received: from smtp6-g21.free.fr ([212.27.42.6]) by mail1-smtp-roc.national.inria.fr with ESMTP; 02 Mar 2010 00:01:50 +0100 Received: from smtp6-g21.free.fr (localhost [127.0.0.1]) by smtp6-g21.free.fr (Postfix) with ESMTP id E7E8CE0801C for ; Tue, 2 Mar 2010 00:01:45 +0100 (CET) Received: from localhost.localnet (88-123-240-180.rev.libertysurf.net [88.123.240.180]) by smtp6-g21.free.fr (Postfix) with ESMTP id 11CC2E0800E for ; Tue, 2 Mar 2010 00:01:43 +0100 (CET) From: Florent Monnier To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] How to pass C pointers to Caml Date: Tue, 2 Mar 2010 00:04:22 +0100 User-Agent: KMail/1.12.2 (Linux/2.6.31.12-desktop586-1mnb; KDE/4.3.2; i686; ; ) References: <9dfe358d1002281955k4cb9207bi4afe5d649fc20e05@mail.gmail.com> <201003010722.15601.monnier.florent@gmail.com> <87tyt0jhsy.fsf@frosties.localdomain> In-Reply-To: <87tyt0jhsy.fsf@frosties.localdomain> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-Id: <201003020004.22232.monnier.florent@gmail.com> X-Spam: no; 0.00; pointers:01 ocaml:01 pointers:01 ocaml:01 pointer:01 pointer:01 trivial:01 allocates:01 bindings:01 struct:01 struct:01 zhao:98 boom:98 abstract:01 abstract:01 Le lundi 1 mars 2010 14:24:45, Goswin von Brederlow a =E9crit : > Florent Monnier writes: > > Le lundi 1 mars 2010 04:55:00, Jianzhou Zhao a =E9crit : > >> I have been calling OCaml code from C in my project. > >> The C code has some pointers to C structures. > >> I got 'seg fault' when calling the OCaml function receiving > >> C structure pointers. > >> > >> 18.7 at http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html > >> gives the examples that pass int into OCaml. These examples work for m= e. > >> But, Does OCaml support to pass C structure pointers to OCaml? > > > > Yes it does. Just cast your pointer to the type value. > > > > In this tutorial there is an example "Pointers to C structures": > > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php#ref_ptr > > > > the pointer to a C struct is wrapped on the ocaml side by an abstract > > type called "t" here, and it is provided back to C with print_t / > > dump_ptr. >=20 > The problem with this trivial approach is that ocaml can store the > pointer somewhere. When the C pointer is freeed then ocaml has a > dangling pointer. Worse, if the GC allocates a new heap then the pointer > might suddenly point into the heap and then BOOM. A lot of bindings wrap C pointer, it is known to be a technic that does wor= k. Dangerous that's true, be if you are very careful, it works. What you can do is set the pointer to NULL when the struct is freed, and th= en=20 each function that uses this struct pointer can first check if the pointer = is=20 NULL or not before to use it, and if it's NULL raise an exception. > It is better to put the pointer into an abstract or custom block. You can do this too. =2D-=20