This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: static constructors/destructors and atexit


> I think that we can assume that this other code is all C (you can't
> mix different C++ compilers), so it may have atexit calls but won't
> have any destructor calls.
> 
> Since this other code will be calling the real atexit(), then the atexit()
> calls for the whole program may not be in the right order.

Here is a sample implementation how static destructors can be
implemented in a semi-sane standard conformant way:

register_destructor(funpter)
{
	int atexitcode =0;
	static int OK_toregister = 1;

	if (OK_toregister)	
		atexitcode = at_exit(trampoline_for_destructor);

	append_to_destructorlist (funptr, atexitcode);
}

trampoline_for_destructor()
{
	*get_from_destructorlist(funptr)();

	if (check_exitcode_for_next_destructor() == 0)
		for (registered destructors)
			*get_from_destructorlist()();
}

This code intersperses destructorcalss w/ other atexit calls while the
system lib can handle it, then pulls off the rest at the end...

m.

-- 
Michael K. Gschwind
IBM T.J. Watson Research Center     phone (external): (914) 945-3162
PO Box 218                          phone (internal): T/L 862-3162
Yorktown Heights, NY 10598          e-mail: mikeg@watson.ibm.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]