atexit

Setting exit code in atexit callback

ぐ巨炮叔叔 提交于 2021-01-27 12:52:52
问题 Is there any way to set exit code in the function registered in atexit module and called on exit? The call to sys.exit(code) produces an error and does not set exit code to the desired value. d:\>python atexit_test.py Error in atexit._run_exitfuncs: Traceback (most recent call last): File "atexit_test.py", line 3, in myexit sys.exit(2) SystemExit: 2 d:\>echo %ERRORLEVEL% 0 The contents of atexit_test.py : def myexit(): import sys sys.exit(2) import atexit atexit.register(myexit) 回答1: I can

valgrind --trace-children=yes reports leak despite atexit cleanup

心已入冬 提交于 2021-01-27 06:09:26
问题 I'm trying to avoid false positives with valgrind, but I'm suck with a combination of atexit() and fork() , despite using --trace-children=yes . My code: #include <stdio.h> #include <unistd.h> #include <stdlib.h> static int * arr; static void cleanup() { free(arr); printf("free arr as: %p\n", (void *)arr); } int main() { arr = malloc(16 * sizeof(int)); printf("allocated arr as: %p\n", (void *)arr); atexit(cleanup); pid_t pid = fork(); if (pid == -1) { exit(1); } else if (pid == 0) { // child

valgrind --trace-children=yes reports leak despite atexit cleanup

非 Y 不嫁゛ 提交于 2021-01-27 06:09:15
问题 I'm trying to avoid false positives with valgrind, but I'm suck with a combination of atexit() and fork() , despite using --trace-children=yes . My code: #include <stdio.h> #include <unistd.h> #include <stdlib.h> static int * arr; static void cleanup() { free(arr); printf("free arr as: %p\n", (void *)arr); } int main() { arr = malloc(16 * sizeof(int)); printf("allocated arr as: %p\n", (void *)arr); atexit(cleanup); pid_t pid = fork(); if (pid == -1) { exit(1); } else if (pid == 0) { // child

Script stuck on exit when using atexit to terminate threads

江枫思渺然 提交于 2020-02-23 04:46:06
问题 I'm playing around with threads on python 3.7.4, and I want to use atexit to register a cleanup function that will (cleanly) terminate the threads. For example: # example.py import threading import queue import atexit import sys Terminate = object() class Worker(threading.Thread): def __init__(self): super().__init__() self.queue = queue.Queue() def send_message(self, m): self.queue.put_nowait(m) def run(self): while True: m = self.queue.get() if m is Terminate: break else: print("Received

What's the best way to register a function to run during an unexpected exit of a Rust program?

不问归期 提交于 2020-01-15 07:28:07
问题 I'm creating a terminal text editor in Rust. The editor puts the terminal into raw mode, disabling character echoing and the like, and then restores the original terminal function upon exit. However, the editor has some bugs, and crashes unexpectedly every now and again due to issues like unsigned variable underflow. When this happens, the cleanup code which would restore the terminal to its original state never runs. The cleanup function I'd like to run is the following: fn restore_orig_mode

Mixed-mode C++/CLI crashing: heap corruption in atexit (static destructor registration)

纵饮孤独 提交于 2020-01-03 15:17:24
问题 I am working on deploying a program and the codebase is a mixture of C++/CLI and C#. The C++/CLI comes in all flavors: native, mixed ( /clr ), and safe ( /clr:safe ). In my development environment I create a DLL of all the C++/CLI code and reference that from the C# code (EXE). This method works flawlessly. For my releases that I want to release a single executable (simply stating that "why not just have a DLL and EXE separate?" is not acceptable). So far I have succeeded in compiling the EXE

Order between destruction of global object and atexit in C++

℡╲_俬逩灬. 提交于 2020-01-03 06:49:20
问题 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() { atexit(onExit); return 0; } I've found onExit() is invoked before MyClass::~MyClass() in Visual Studio 2012 and gcc4.7.2. Am I sure that onExit is always invoked before global object(like g_class ) destruction? I wonder global object register order and atexit register

killing child processes at parent process exit

喜欢而已 提交于 2019-12-22 16:36:31
问题 I'm very new to c and programming and need some help. In c on linux(cygwin) I am required to remove all child processes at exit. I have looked at the other similar questions but can't get it to work. I've tried- atexit(killzombies); //in parent process void killzombies(void) { printf("works"); kill(0, SIGTERM); printf("works"); if (waitpid(-1, SIGCHLD, WNOHANG) < 0) printf("works"); } for some reason, "works" doesn't even print ever. I press ctrl + c to exit. ALSO I have tried- prctl(PR_SET

killing child processes at parent process exit

那年仲夏 提交于 2019-12-22 16:34:03
问题 I'm very new to c and programming and need some help. In c on linux(cygwin) I am required to remove all child processes at exit. I have looked at the other similar questions but can't get it to work. I've tried- atexit(killzombies); //in parent process void killzombies(void) { printf("works"); kill(0, SIGTERM); printf("works"); if (waitpid(-1, SIGCHLD, WNOHANG) < 0) printf("works"); } for some reason, "works" doesn't even print ever. I press ctrl + c to exit. ALSO I have tried- prctl(PR_SET

vfork() atexit assertion failed

老子叫甜甜 提交于 2019-12-20 05:39:28
问题 I am trying to understand the following piece of code #include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { pid_t pid ; unsigned int i=0; pid=vfork(); switch(pid) { case -1: // some sort of error puts("fork error"); break; case 0: // the child process while(i<100) { printf("%d\n", i); i++; } break; default: //parent while(i<1000) { printf("%d\n", i); i++; } break; } // _exit(0); } And please don't tell me that vfork() is bad and these kind of things .I know it is , but what