atexit(3) is a method for arranging a function to be called at a program's exit time.
Questions tagged [atexit]
162 questions
30
votes
2 answers
How to find exit code or reason when atexit callback is called in Python?
I want to know if a Python script is terminating correctly or not. For this I am using atexit but the problem is that I do not know how to differentiate if atexit was called with sys.exit(0) or non zero or an exception.
Reasoning: if program ends…

sorin
- 161,544
- 178
- 535
- 806
24
votes
4 answers
Run atexit() when python process is killed
I have a python process which runs in background, and I would like it to generate some output only when the script is terminated.
def handle_exit():
print('\nAll files saved in ' + directory)
…

Chan Jing Hong
- 2,251
- 4
- 22
- 41
24
votes
1 answer
Order between destruction of global object and atexit in C++
I wonder that can sure order between destruction of global object and atexit in C++
I have a global object and register atexit function like below:
static MyClass g_class;
void onExit()
{
// do some destruction
}
int main()
{
…

zelon
- 297
- 2
- 8
23
votes
1 answer
dlclose() does not call the destructor of global objects
plugin1.cpp:
#include
static class TestStatic {
public:
TestStatic() {
std::cout << "TestStatic create" << std::endl;
}
~TestStatic() {
std::cout << "TestStatic destroy" << std::endl;
}
} test_static;
host.cpp
#include…

AndryBlack
- 233
- 2
- 6
22
votes
1 answer
Python Process won't call atexit
I'm trying to use atexit in a Process, but unfortunately it doesn't seem to work. Here's some example code:
import time
import atexit
import logging
import multiprocessing
logging.basicConfig(level=logging.DEBUG)
class W(multiprocessing.Process):
…

Brian M. Hunt
- 81,008
- 74
- 230
- 343
18
votes
2 answers
Python Multiprocessing atexit Error "Error in atexit._run_exitfuncs"
I am trying to run a simple multiple processes application in Python. The main thread spawns 1 to N processes and waits until they all done processing. The processes each run an infinite loop, so they can potentially run forever without some user…
ShimmerTroll
17
votes
2 answers
ruby at_exit exit status
Can i determine selves process exit status in at_exit block?
at_exit do
if this_process_status.success?
print 'Success'
else
print 'Failure'
end
end

tig
- 25,841
- 10
- 64
- 96
15
votes
2 answers
How to register "atexit" function in python's multiprocessing subprocess?
I have some subprocesses (using multiprocessing) and when they stop, each of them need do some final work. Something like the following, which did not work though...
import multiprocessing
import atexit
def final():
print "final work"
def…

rongdong.bai
- 471
- 1
- 6
- 16
14
votes
3 answers
How to register a non-void function with atexit()?
I'm trying to register a function that returns an int to be called at the end of a program using the atexit() function. (Specifically, the endwin() function from ncurses.)
But since atexit() needs a pointer to a void function, I ran into a problem.…

Emil Laine
- 41,598
- 9
- 101
- 157
14
votes
2 answers
Can I undo or remove an atexit command?
If I place atexit( fn ); on the exit stack, it will get executed when the program exits: returns from main() or via exit().
Can I remove it from the stack?
Why do I want to do this, you ask?
I was experimenting with a simple try-catch mechanism…

philcolbourn
- 4,042
- 3
- 28
- 33
13
votes
8 answers
Freeing in an atexit()
Is there any point to freeing memory in an atexit() function?
I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits…

Matt
- 84,419
- 25
- 57
- 67
12
votes
1 answer
When is a function registered with atexit() called
I want to know if functions registered with atexit() are called before or after global variables are destroyed. Is this specified by the standard or implementation defined?

Mircea Ispas
- 20,260
- 32
- 123
- 211
11
votes
1 answer
What does the POSIX standard say about thread stacks in atexit() handlers? What's the OS practice?
When our UNIX/C program needs an emergency exit, we use exit (3) function and install atexit (3) handlers for emergency clean-ups. This approach worked fine until our application got threaded, at which point atexit() handlers stopped to work…

Kostja
- 1,607
- 10
- 17
11
votes
2 answers
What are the implications of registering an instance method with atexit in Python?
Assume I've got some really big Python class that might consume a fair amount of memory. The class has some method that is responsible for cleaning up some things when the interpreter exits, and it gets registered with the atexit module:
import…

Jed
- 1,011
- 1
- 9
- 15
10
votes
3 answers
Mixed-mode C++/CLI DLL throws exception on exit
I am having a problem with a C++/CLI mixed mode DLL that I created. It is throwing an exception when unloading as the .NET application that uses it exits. After DLL_PROCESS_DETACH is executed, the DLL does runtime clean-up using automatically…

Mark
- 510
- 1
- 5
- 18