yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
363 stars 95 forks source link

Exception: Ctypes_ffi_stubs.CallToExpiredClosure #713

Closed vrotaru closed 1 year ago

vrotaru commented 1 year ago

Hi, I'm currently playing with a proof-of-concept Gtk4 bindings and I've run into this issue CallToExpiredClosure exception being thrown. It happens when I let window open a rather long time, and here is the closure

action_save#activate <~> (fun _ ->
     Out_channel.(
         if not readonly then
            with_open_bin path (fun ch -> output_string ch buffer#text)
     );
     self#application#quit ()
);

Where <~> is just a shortcut for signal_connect and (fun _ ...) is the callback. Is that something expected? Now that I think of it probably yes, since buffer is local to the method were this is called. Is that the case?

yallop commented 1 year ago

Is that something expected?

It's expected (though not guaranteed) if the closure passed to C is garbage collected before it's called from the C side. It should be possible to avoid the problem by keeping a reference of some kind to the closure in OCaml, but the details will depend on your library or application.

vrotaru commented 1 year ago

Thanks, for the reply. I'll try to check it by forcing a garbage collection.