CallbackRegistry
class optris_sdk.callbacks.CallbackRegistry
Keeps ctypes thunks alive for as long as the SDK may call them.
This is not bookkeeping for its own sake. ctypes frees the machine
code behind a CFUNCTYPE instance when the Python object is
collected, and the SDK holds a raw pointer to it that it has no way
of knowing has gone. A registered callback whose thunk was collected
is a jump into freed memory on the next frame -- a segfault with no
Python traceback, some seconds after the line that caused it.
So every thunk is held here, on the camera, and released only in
Camera.close() -- which is after optris_sdk_imager_destroy
has joined the SDK's threads and guaranteed no further callback can
fire.
CallbackRegistry — methods
hold(slot, thunk)
Keep thunk alive in slot, and the one it replaces too.
The one it replaces is not dropped. hold() runs before the
setter that hands the new thunk to the SDK, and a frame due in
between is delivered through the old one; and a setter is not a
fence either, because the SDK copies a callback out from under
its lock and calls it after the lock is released. Dropping the
old thunk here freed it while the SDK could still be inside it:
measured on a simulated camera at 1000 Hz, a program that set
on_thermal_frame again and again crashed with SIGSEGV in 3
of 3 runs within five seconds. So the replaced thunk is kept
until release, after the camera has been joined. A
program that replaces a callback on every frame keeps one small
object per replacement until it closes the camera.
release()
Drop every thunk. Only safe once the SDK has been joined.
release_slot(slot)
Drop one slot, if it is held.
Only for a slot whose SDK-side removal has already
returned. The registered-buffer calls are the case this
exists for: optris_sdk_imager_remove_external_buffer()
documents that it returns once the SDK will no longer write
there, so after it has returned the thunk and the buffer behind
that slot are genuinely unreachable. For anything registered by
a plain setter, clearing it with NULL is not always a fence
-- the reconnect-attempt veto is copied out from under its lock
and runs to completion afterwards -- so those stay held until
the camera is destroyed.
release_prefix(prefix)
Drop every slot whose name starts with prefix.
The same rule as release_slot: only once the SDK-side
removal has returned.