On Sun, 21 Dec 2025 10:15:37 +0000
David Brownlee <[email protected]> wrote:

> I fear this may be a "de facto vs de jure" case. If the coverage of
> the official standard is deficient, and software is being written to
> expect the (not unreasonable) implemented behaviour of most systems,
> then implementing the latter is likely the best option.

I think the standard is very clear:

"... to be called without arguments from exit(), or after return from
the initial call to main(), or on the last thread termination."

The fact that the standard does not cover all the other use cases that
some people may wish to implement is beside the point. The common
wisdom is that atexit() was never designed for cleanup handlers that
may later point to dlclose()ed shared libraries.

Some people may read the standard and design their applications in such
ways that atexit() cleanup handlers installed by DSOs are referencing
symbols in the main process address space and expect those handlers to
run only when the process is about to terminate, since other threads
may be actively using allocated resources. Running the cleanup handlers
on dlclose() could crash such applications. Not running them would leak
resources.

I still don't understand why anybody would want to call atexit() from
DSOs. If the design calls for dlopen()/dlclose() then simply provide
cleanup functions and document the fact that people must execute them
prior to dlclose(). There is no RAII is C and atexit() is not meant to
be used as such.

Reply via email to