Page 1 of 1

C-style atexit functions?

Posted: Sun Nov 02, 2014 3:36 pm
by 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.

Re: C-style atexit functions?

Posted: Thu Jan 08, 2015 3:46 am
by ssqq
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.

Re: C-style atexit functions?

Posted: Tue Mar 03, 2015 9:52 pm
by ralph.ronnquist
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:

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?

Posted: Wed Mar 04, 2015 12:14 am
by Lutz
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!

Re: C-style atexit functions?

Posted: Wed Mar 04, 2015 6:33 am
by ralph.ronnquist
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:
ERR: 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.