This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/28017] New: lack of guard variables for explicitly instantiated template static data
- From: "hhinnant at apple dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 13 Jun 2006 19:09:07 -0000
- Subject: [Bug c++/28017] New: lack of guard variables for explicitly instantiated template static data
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
I was passed a test case consisting of two translation units:
// test.h
#include <iostream>
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<typename T>
class Test
{
public:
Test()
{
sA.f();
}
private:
static A sA;
};
template<typename T>
A Test<T>::sA;
// test.cpp
#include "test.h"
template class Test<int>;
int foo()
{
return 0;
}
// main.cpp
#include "test.h"
int foo();
int main()
{
Test<int> theTest;
foo();
return 0;
}
When compiled with something like:
g++ -o test test.cpp main.cpp
the test shows evidence of the static Test<int>::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