Topic: atexit and static destruction
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/14 Raw View
jhl@sssonline.com (Jonathan H Lundquist) writes:
>I think a much better resolution is to define the semantics of atexit
>and static destructors in a stack-like fashion.
The C89 standard does not say what happens if a function called
from exit processing itself registers a function with atexit.
The C++ standard is self-contradictory on the subject, and on
the subject of static objects in functions first called from
an atexit-registered function.
C9X (soon to be released as an official standard) has fixed the
definition of exit processing to require stack-like behavior.
A Defect Report was filed last year on the C++ standard, and
the recommended handling is also stack-like behavior.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jhl@sssonline.com (Jonathan H Lundquist)
Date: 1999/10/14 Raw View
I just read
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#3, which is
particularly interesting, as this is a problem I had to write a patch
to the MSVC runtime library for some time back. While I totally agree
with the description of the problem, I certainly don't like the
proposed resolution. If this resolution is adopted it will be
extremely difficult to prove that any program which uses local scope
static variables does not exhibit undefined behavior during
termination via exceptional code paths.
I think a much better resolution is to define the semantics of atexit
and static destructors in a stack-like fashion. This has the added
advantage of being very easy to implement, patching the MSVC runtime
library to work in this fashion resulted in much more straightforward
code than the original broken implementation.
Under this resolution with the example given:
void F() { static T t; } // type T has a destructor
int main()
{
atexit(F); // the only use of F
}
The destructor for t would be pushed onto the top of the atexit stack,
and would execute as soon as function F completed.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]