Closed edwintorok closed 2 years ago
Ah yes I once asked on ctypes mailing list what was happening with callbacks but didn't get any answer (unfortunately the links in that message are outdated, github should not provide links to master by default). So I now have some of my answers.
Before doing what you suggest I need to check whether multiple debug callbacks can be installed by the client in which case I'll let the burden of keeping a reference on the client of the library. There also stub generation coming to ctypes that may change the game and ask a few questions (e.g. do we still get the dlsym lookup for free, which is essential for OpenGL) so I'll wait and see for now.
@dbuenzli FYI you can use the "y" shortcut to make github expand the URL to its canonical form.
I know, but I often forget to to so. Github is providing the wrong default, when you are making URI references you most often want to refer to a stable resource.
Sorry to bother anyone
But I reliably hit "Ffi_stubs.CallToExpiredClosure" every single time I attach debug_message_callback
with Tgl4
I'm sorry I absolutely don't have any of the context here in my head. If something needs to be done on the tgls side I'll gladly take a patch.
Other than that @gantsevdenis, do you keep a reference on your side to the exact closure you register ? That, I guess should solve the problem.
Ah but you I guess you can't since it's on wrap_cb
that the root should be kept.
The lifetime of funptr
and its behaviour wrt to OCaml runtime lock is now documented: https://github.com/ocamllabs/ocaml-ctypes/blob/9048ac78b885cc3debeeb020c56ea91f459a4d33/src/ctypes-foreign/foreign.mli#L80-L85.
There is a dynamic_funptr
with explicit alloc/dealloc that can be used to avoid the GC freeing things too soon. Otherwise see just how complicated it is to carefully keep references to everything to avoid the expired closure exception: https://github.com/ocamllabs/ocaml-ctypes/blob/261fe071fad17ab323d8d2b82df2aec593e64e3f/tests/test-callback_lifetime/test_callback_lifetime.ml#L108-L136
I've opened a PR to attempt to fix this, including a slightly adjusted version of the above testcase, and I now have OpenGL4 capable hardware to test it on, and seems to have worked fine.
@gantsevdenis could you tell us if that works you by trying with opam pin tgls --dev
@dbuenzli I confirm it totally works now!
I don't have OpenGL 4.x capable hardware, but in my experiments with the ARB_debug_output extension on 3.x I got an Ffi_stubs.CallToExpiredClosure.
The wrap_cb in the following function (which is identical to the one in Tgl4) can be garbage collected (it is used as a weak reference in ctypes):
I worked it around by storing the callback in a global:
Here is a testcase for Tgl4: