C-style atexit functions?

Started by ryuo, November 02, 2014, 07:36:11 AM

Previous topic - Next topic

ryuo

I've been wondering if you would be willing to add a feature like ISO C's atexit function. Basically, the ability to register functions to be executed when someone calls (exit) from within newLISP or similar. This could be useful for cleaning up global symbols inside a module context or program. I'm thinking stuff that was allocated by an imported function, which is not managed by newLISP.



If this is not a good idea for a builtin, then I could just emulate it with a wrapper to the exit function I suppose. But, I figured it would be better if it was handled by newLISP. Anyway, thanks.

ssqq

#1
yes, When we import some module, imported symbols list is useful, when it is not useable, delete it from symbols table also is a good habbit.

rrq

#2
This may well be too platform dependent, but on Linux (Ubuntu 12.04) one can augment the unix.lsp module with an import of on_exit and then register a callback to be performed on exit. Example:
(module "unix.lsp")
(import unix:library "on_exit")
(on_exit (callback 0 'on-exit) 0)
(define (on-exit) (println "Cheerio!"))




This, as it should, invokes the on-exit function when the process exits. And it works well togeter with signal handlers such as for instance:
(define (handler x) (exit 1))
(signal 2 handler) ; QUIT
(signal 15 handler) ; TERM

Lutz

#3
on Mac OS X and other BSDs use atexit(...):



> (import "libc.dylib" "atexit")
atexit@7FFF833285B1
> (atexit (callback 0 'on-exit))
0
> (define (on-exit) (println "Cheerio!"))
(lambda () (println "Cheerio!"))
> (exit)
Cheerio!

rrq

#4
Yes. The Linux man pages also speak highly about using atexit in preference of on_exit, but for some reason I can't import it. By nm, the library symbol table has atexit as "text" item, but when trying to import it I get the complaint:
QuoteERR: import function not found in function import : "/lib/i386-linux-gnu/libc.so.6: undefined symbol: atexit"

Peculiar. But on_exit imports without complaints, and apparently does the same thing.