public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hhinnant at apple dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/28017]  New: lack of guard variables for explicitly instantiated template static data
Date: Tue, 13 Jun 2006 19:13:00 -0000	[thread overview]
Message-ID: <bug-28017-11686@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2006-06-13 19:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-13 19:13 hhinnant at apple dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-28017-11686@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).