Difference between revisions of "Execute functions before exiting"
Jump to navigation
Jump to search
(Created page with " Sometimes it is necessary to execute functions when slang/isis is closed. In slang scripts this can be easily done via the <code>atexit</code> function. For example: <pre>...") |
|||
Line 4: | Line 4: | ||
For example: | For example: | ||
− | < | + | <syntaxhighlight lang="c" line='line'> |
define my_exit_function () { vmessage("Exit time: %s", strftime("%H:%M:%S")); } | define my_exit_function () { vmessage("Exit time: %s", strftime("%H:%M:%S")); } | ||
atexit(&my_exit_function); | atexit(&my_exit_function); | ||
− | </ | + | </syntaxhighlight> |
will print the exit time of the slang/isis process to <code>stdout</code>. | will print the exit time of the slang/isis process to <code>stdout</code>. | ||
Line 16: | Line 16: | ||
The same behaviour can be accomplished in an slang module written in C. The function that needs to be called gets appended via | The same behaviour can be accomplished in an slang module written in C. The function that needs to be called gets appended via | ||
− | < | + | <syntaxhighlight lang="c" line='line'> |
void my_module_exit_function (void) { | void my_module_exit_function (void) { | ||
destroy(global_object); | destroy(global_object); | ||
Line 23: | Line 23: | ||
SLang_add_cleanup_function(&my_module_exit_function); | SLang_add_cleanup_function(&my_module_exit_function); | ||
− | </ | + | </syntaxhighlight> |
This might be necessary when memory is allocated and used until the very end. | This might be necessary when memory is allocated and used until the very end. | ||
[[Category:Isis / Slang]] | [[Category:Isis / Slang]] |
Revision as of 11:08, 13 August 2018
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!");
4}
5
6SLang_add_cleanup_function(&my_module_exit_function);
This might be necessary when memory is allocated and used until the very end.