<div dir="ltr"><div><div>Hello,<br><br></div>here is minimal example, which works without any problems on x86 and raises CallToExpiredClosure on ARM:<br><a href="https://github.com/mryau/ctypes-mintest">https://github.com/mryau/ctypes-mintest</a><br><br></div>WBR, ssp<br></div><div class="gmail_extra"><br><div class="gmail_quote">2017-05-11 16:37 GMT+05:00 Jeremy Yallop <span dir="ltr"><<a href="mailto:yallop@gmail.com" target="_blank">yallop@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 11 May 2017 at 10:20, Serge Sivkov <<a href="mailto:ssp.mryau@gmail.com">ssp.mryau@gmail.com</a>> wrote:<br>
> thank for your reply, it allow me partially resolve problem: code works on<br>
> x86 without any problems (yet with Gc.compact() calls insert after each line<br>
> of code). But ARM version allways raises CallToExpiredClosure on the first<br>
> call to callback.<br>
> I've tried next variants to store callback if struct field:<br>
> - existing toplevel function by name<br>
> - store function name to toplevel ref and save it to struct field by<br>
> !ref_name<br>
> - by register closure to function (fun a b c d -> top_level_function a b c<br>
> d) with Ctypes.Root.create<br>
> - by store same closure to Hashtbl.<br>
> - by register finaliser with Gc.finalise for closure (and it has not been<br>
> called)<br>
> All this variants work on x86 and do not work on ARM, i.e. first call to<br>
> OCaml callback from C raises CallToExpiredClosure.<br>
> It seems I don't understand problem at all.<br>
><br>
> So here is base questions:<br>
> - in case I store in C struct field existing toplevel function by name, may<br>
> it be GC'ed?<br>
<br>
</span>Yes.  The only way to prevent a value from being collected is to<br>
ensure that it's visible to the GC, which requires keeping a reference<br>
to it in an OCaml object.  The GC doesn't scan C memory, such as<br>
struct fields, so storing a function in a struct field won't stop it<br>
being collected.<br>
<span class=""><br>
> -what may cause CallToExpiredClosure raise too if I has only one callback to<br>
> call and at C side this struct in GDB seems to be correct?<br>
<br>
</span>If there's an OCaml reference to the function passed to C then<br>
CallToExpiredClosure should never occur.  But it has to be exactly the<br>
same function -- for example, storing a function in a struct field,<br>
then reading from the struct field will create a new copy of the<br>
function, which won't prevent the original from being collected.<br>
<br>
If you have a (smallish) running example somewhere of the code that's<br>
causing problems I'd be happy to take a look.<br>
<br>
Kind regards,<br>
<br>
Jeremy.<br>
</blockquote></div><br></div>