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.
C-style atexit functions?
Re: C-style atexit functions?
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.
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: C-style atexit functions?
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:
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:
Code: Select all
(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:
Code: Select all
(define (handler x) (exit 1))
(signal 2 handler) ; QUIT
(signal 15 handler) ; TERM
Re: C-style atexit functions?
on Mac OS X and other BSDs use atexit(...):
Code: Select all
> (import "libc.dylib" "atexit")
atexit@7FFF833285B1
> (atexit (callback 0 'on-exit))
0
> (define (on-exit) (println "Cheerio!"))
(lambda () (println "Cheerio!"))
> (exit)
Cheerio!
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: C-style atexit functions?
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:
Peculiar. But on_exit imports without complaints, and apparently does the same thing.ERR: import function not found in function import : "/lib/i386-linux-gnu/libc.so.6: undefined symbol: atexit"