caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Florent Monnier <monnier.florent@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] How to pass C pointers to Caml
Date: Tue, 02 Mar 2010 11:19:58 +0100	[thread overview]
Message-ID: <87vddfhvox.fsf@frosties.localdomain> (raw)
In-Reply-To: <201003020004.22232.monnier.florent@gmail.com> (Florent Monnier's message of "Tue, 2 Mar 2010 00:04:22 +0100")

Florent Monnier <monnier.florent@gmail.com> writes:

> Le lundi 1 mars 2010 14:24:45, Goswin von Brederlow a écrit :
>> Florent Monnier <monnier.florent@gmail.com> writes:
>> > Le lundi 1 mars 2010 04:55:00, Jianzhou Zhao a écrit :
>> >> 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 me.
>> >> 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.
>> 
>> 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 work.
> 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 then 
> each function that uses this struct pointer can first check if the pointer is 
> NULL or not before to use it, and if it's NULL raise an exception.

let x = ref None

let called_function c_ptr = x := Some c_ptr

How will you get x to be Some NULL? Your C code does not know about the
copy. You need to wrap the C pointer into a custom or abstract block
first to be able to NULL it. A finalizer in a custom block can also be
helpfull here and free the pointer when ocaml no longer needs it.

>> It is better to put the pointer into an abstract or custom block.
>
> You can do this too.

Imho you must. Anything else is too dangerous.

MfG
        Goswin


  reply	other threads:[~2010-03-02 10:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-01  3:55 Jianzhou Zhao
2010-03-01  6:22 ` [Caml-list] " Florent Monnier
2010-03-01 13:23   ` Jianzhou Zhao
2010-03-01 13:24   ` Goswin von Brederlow
2010-03-01 23:04     ` Florent Monnier
2010-03-02 10:19       ` Goswin von Brederlow [this message]
2010-03-02 11:34         ` Florent Monnier
2010-03-03 11:02           ` Goswin von Brederlow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vddfhvox.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=monnier.florent@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).