Execute functions before exiting

From Remeis-Wiki
Revision as of 11:10, 13 August 2018 by Stierhof (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Sometimes it is necessary to execute functions when slang/isis is closed. In slang scripts this can be easily done via the atexit function.

For example:

1define my_exit_function () { vmessage("Exit time: %s", strftime("%H:%M:%S")); }
2
3atexit(&my_exit_function);

will print the exit time of the slang/isis process to stdout.

Exit function in C

The same behaviour can be accomplished in an slang module written in C. The function that needs to be called gets appended via

1void my_module_exit_function (void) {
2    destroy(global_object);
3    printf("Cleaning up mess!\n");
4}
5
6SLang_add_cleanup_function(&my_module_exit_function);

This might be necessary when memory is allocated and used until the very end.