diff --git a/src/ctxt/ctxt.c b/src/ctxt/ctxt.c index 6a27dce..a951b57 100644 --- a/src/ctxt/ctxt.c +++ b/src/ctxt/ctxt.c @@ -22,8 +22,10 @@ struct ax_ctxt* ax_new(void) void ax_free(struct ax_ctxt* ax) { - ax__ctxt_cleanup(ax); - rgn_cleanup(ax->init_rgn); + if (ax != NULL) { + ax__ctxt_cleanup(ax); + rgn_cleanup(ax->init_rgn); + } } const char* ax_get_error(struct ax_ctxt* ax) diff --git a/test/main.c b/test/main.c index 0889cc9..6afb806 100644 --- a/test/main.c +++ b/test/main.c @@ -1,12 +1,30 @@ #include +#include +#include #include "../src/ax.h" #include "../src/ctxt.h" #include "../src/backend.h" +struct ax_ctxt* ax = NULL; + +static void cleanup() +{ + ax_free(ax); + ax = NULL; +} + +static void sigint_handler(int signum) +{ + printf("\nCaught interrupt.\n"); + cleanup(); + exit(0); +} int main(void) { - struct ax_ctxt* ax = ax_new(); + signal(SIGINT, sigint_handler); + + ax = ax_new(); ax_set_logger(ax, 0, false); int rv = 0; @@ -22,13 +40,15 @@ int main(void) ax_log(ax, "Got here\n"); - ax__backend_wait_for_event(ax->bac); - ax__backend_step(ax->bac); + while (!ax__backend_is_shutdown(ax->bac)) { + ax__backend_wait_for_event(ax->bac); + ax__backend_step(ax->bac); + } //cleanup: if (rv != 0) { printf("error: %s\n", ax_get_error(ax)); } - ax_free(ax); + cleanup(); return rv; }