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]
Other format: [Raw text]

multiple destructor calls of static objects


Hello,

I ran into a bug with 4.6, when implementing a custom rtti framework, on an Amd64 Debian Wheezy box. It seems that at certain optimization levels, dtors of static objects are called multiple times. Attached a test case. It will print 'DEADBEEF' when the bug is hit. Searched bugzilla for "multiple destructor call", but found nothing relevant. The bug appears with only 4.6.x, and only at -O2, 3 and s.

Tested with:
* gcc-4.4.7 20111231 (prerelease) (GCC), compiled from source
* gcc-4.5.3, stock debian wheezy
* gcc-4.6.2, stock debian wheezy
* gcc-4.6.3 20120120 (prerelease) (GCC), compiled from source
* gcc-4.7.0 20120104 (experimental) (GCC), compiled from source

Should I file a bugreport and / or search for the commit that introduced the bug?

Regards, Peter


#include <stdio.h>

struct Info
{
        Info(int v_ = 0) : 
		alive(1), 
		var(v_) 
	{
		printf("%p Info(%d)\n", this, v_); 
	}

        ~Info() 
	{ 
		if (alive) { 
			printf("%p ~Info(%d)\n", this, var); 
			alive = 0; 
		} else {
			printf("%p ~Info(%d) : DEADBEEF!\n", this, var); 
		} 
	}

        int alive;
        int var;
};


template<typename T>
inline
const Info& foo()
{
        static Info info;
        return info;
}

template<>
inline
const Info& foo<int>() __attribute__((constructor));
template<>
inline
const Info& foo<int>()
{
        static Info info(1);
        return info;
}

template<>
inline
const Info& foo<char>() __attribute__((constructor));
template<>
inline
const Info& foo<char>()
{
        static Info info(2);
        return info;
}

int main()
{
	printf("> main()\n");
// if both #if's are 1, the dtors will be called multiple times
// if compiled w/ gcc 4.6, -Os, -O2 or -O3. 
// -O0 and -O1 are OK
#if 1
	foo<char>();
	foo<int>();
	foo<float>();
#endif

#if 1
	foo<char>();
	foo<int>();
	foo<float>();
#endif
	printf("< main()\n");
	return 0;
}

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