From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14995 invoked by alias); 13 Jun 2006 19:09:14 -0000 Received: (qmail 14978 invoked by uid 48); 13 Jun 2006 19:09:07 -0000 Date: Tue, 13 Jun 2006 19:13:00 -0000 Subject: [Bug c++/28017] New: lack of guard variables for explicitly instantiated template static data X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "hhinnant at apple dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2006-06/txt/msg01299.txt.bz2 List-Id: I was passed a test case consisting of two translation units: // test.h #include class A { public: A() { mString = new char[2]; std::cout << "new: mString has value " << (void*) mString << std::endl; } ~A() { std::cout << "delete: mString has value " << (void*) mString << std::endl; delete [] mString; } void f() { } private: char* mString; }; template class Test { public: Test() { sA.f(); } private: static A sA; }; template A Test::sA; // test.cpp #include "test.h" template class Test; int foo() { return 0; } // main.cpp #include "test.h" int foo(); int main() { Test theTest; foo(); return 0; } When compiled with something like: g++ -o test test.cpp main.cpp the test shows evidence of the static Test::sA being constructed/destructed twice: new: mString has value 0x5002d0 new: mString has value 0x500300 delete: mString has value 0x500300 delete: mString has value 0x500300 The desired output should be more like: new: mString has value 0x5002d0 delete: mString has value 0x5002d0 Further inspection reveals that the guard variable for this static is not being generated in test.cpp because of the explicit instantiation there (tested on Apple's gcc 4.0.1). I am wondering if an appropriate fix might be in: gcc/cp/decl2.c, in function start_static_initialization_or_destruction Change the if statement that looks like: if (TREE_PUBLIC (decl) && (DECL_COMMON (decl) || DECL_ONE_ONLY (decl) || DECL_WEAK (decl))) { tree guard_cond; to: if (TREE_PUBLIC (decl) && (DECL_COMMON (decl) || DECL_ONE_ONLY (decl) || DECL_WEAK (decl) || DECL_EXPLICIT_INSTANTIATION (decl))) { tree guard_cond; I looked in the mainline decl2.c and found the same logic in the macro NEEDS_GUARD_P. Here I'm suggesting changing: #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \ || DECL_ONE_ONLY (decl) \ || DECL_WEAK (decl))) to: #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \ || DECL_ONE_ONLY (decl) \ || DECL_WEAK (decl) \ || DECL_EXPLICIT_INSTANTIATION (decl))) -- Summary: lack of guard variables for explicitly instantiated template static data Product: gcc Version: 4.0.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: hhinnant at apple dot com GCC host triplet: darwin ppc http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28017