Search code examples
c++g++glibcbionic

atexit() function bionic vs glibc


I found interesting moment: the atexit() function works differently for bionic and glibc. Here is an example:

#include <cstdlib>
#include <cstdio>
extern "C" {
    void one(){
    printf("one\n");
    }
    void two() {
    printf("two\n");
    atexit(one);
    }
}
int main() {
    atexit(two);
}

Results for bionic:

two

Results for glibc:

two
one

Why do the results differ?


Solution

  • It is unspecified whether a call to the atexit function that does not happen before the exit function is called will succeed.

    ISO C standard, §7.22.4.2. So both behaviors are compliant; you can't reliably register a function using atexit while exit is already running its atexit handlers.