public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/28017]  New: lack of guard variables for explicitly instantiated template static data
@ 2006-06-13 19:13 hhinnant at apple dot com
  2006-06-13 19:15 ` [Bug c++/28017] " pinskia at gcc dot gnu dot org
                   ` (12 more replies)
  0 siblings, 13 replies; 16+ messages in thread
From: hhinnant at apple dot com @ 2006-06-13 19:13 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-04-03 23:13 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-28017-4@http.gcc.gnu.org/bugzilla/>
2024-04-03 23:13 ` [Bug c++/28017] lack of guard variables for explicitly instantiated template static data pinskia at gcc dot gnu.org
2006-06-13 19:13 [Bug c++/28017] New: " hhinnant at apple dot com
2006-06-13 19:15 ` [Bug c++/28017] " pinskia at gcc dot gnu dot org
2006-06-13 19:18 ` pinskia at gcc dot gnu dot org
2006-06-13 19:19 ` pinskia at gcc dot gnu dot org
2006-06-13 19:39 ` pinskia at gcc dot gnu dot org
2006-06-13 21:24 ` hhinnant at apple dot com
2006-06-13 21:25   ` Andrew Pinski
2006-06-13 21:41 ` pinskia at physics dot uc dot edu
2006-06-13 21:47 ` hhinnant at apple dot com
2006-06-13 21:47   ` Andrew Pinski
2006-06-13 22:02 ` pinskia at physics dot uc dot edu
2006-06-13 23:28 ` hhinnant at apple dot com
2006-06-19 18:22 ` hhinnant at apple dot com
2006-06-21  5:05 ` pinskia at gcc dot gnu dot org
2006-06-21  5:08 ` pinskia at gcc dot gnu dot org
2009-01-27 17:26 ` bkoz at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).