[ocaml-ctypes] ocaml as callback to C code with event loop

Serge Sivkov ssp.mryau at gmail.com
Thu May 11 10:20:44 BST 2017


Hello,

thank for your reply, it allow me partially resolve problem: code works on
x86 without any problems (yet with Gc.compact() calls insert after each
line of code). But ARM version allways raises CallToExpiredClosure on the
first call to callback.
I've tried next variants to store callback if struct field:
- existing toplevel function by name
- store function name to toplevel ref and save it to struct field by
!ref_name
- by register closure to function (fun a b c d -> top_level_function a b c
d) with Ctypes.Root.create
- by store same closure to Hashtbl.
- by register finaliser with Gc.finalise for closure (and it has not been
called)
All this variants work on x86 and do not work on ARM, i.e. first call to
OCaml callback from C raises CallToExpiredClosure.
It seems I don't understand problem at all.

So here is base questions:
- in case I store in C struct field existing toplevel function by name, may
it be GC'ed?
-what may cause CallToExpiredClosure raise too if I has only one callback
to call and at C side this struct in GDB seems to be correct?

WBR, ssp

2017-05-04 23:24 GMT+05:00 Daniel Bünzli <daniel.buenzli at erratique.ch>:

> On Thursday, 4 May 2017 at 19:50, Serge Sivkov wrote:
> > How can I protect struct handler and fhandler function pointer from
> garbage collection?
>
> I recently struggled with this on test code aswell. One thing that seems
> to work quite well is to store them in a toplevel  reference.
>
> let fhandler = ref fhandler
>
> Best,
>
> Daniel
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20170511/2735eabe/attachment.html>


More information about the Ctypes mailing list