Hi,
I'm having trouble with object lifetimes. My (dumbed down) application looks something like this:
#include <fio.h>
struct my_context { /* ... */ };
void on_timer(void *) { /* ... */ }
void on_finish(void *udata)
{
struct my_context *ctx = udata;
do_some_last_thing_with(ctx);
}
int main()
{
struct my_context ctx;
init(&ctx);
fio_run_every(5000, 1, on_timer, &ctx, on_finish);
fio_start(.threads=1);
fini(&my_context);
}
I should add, that I link it dynamically to libfacil.so
.
When the application is interrupted by e.g. SIGINT, my understanding is that facil.io
's custom signal handler will cause fio_start()
to return to main
. After main
the atexit(3)
handlers run, including those that call _dl_fini
which calls:
|
static void __attribute__((destructor)) fio_lib_destroy(void) { |
Now, there is still the
on_timer
active and will be shutdown by calling
on_finish
here:
after main
already called
fini()
on
ctx
, leading to an access violation in
on_finish
.
Is this the intended behaviour and, if so, how am I using facil.io
in the wrong way? My expectation was that on_finish
(and actually the entire shutdown procedures from fio_lib_destroy
) would be called before fio_start()
returns and not when unloading libfacil.so
.
I realize this is relatively easy to fix either by dynamic allocation of ctx
and something like reference-counting, or by manually dlopen
/dlclose
, but I am wondering whether there was a way to avoid both of these. Maybe by exposing some kind of fio_init()
and fio_fini()
, which I could call around fio_start()
?
Please forgive me if this issue is already covered by the documentation, I couldn't find any hint regarding timers or interaction of the .on_unsubscribe
callbacks with fio_start()
or the shutdown procedure of facil.io
.