www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - static object constructors not being invoked in a DLL

↑ ↓ ← Scott Michel <scottm cs.ucla.edu> writes:
Here's the simplified code example that demonstrates that static template
objects do not get constructed when its DLL get's loaded. Unfortunately, I
have a lot of these in my code and the problem affects me critically.

---------------------------------------------------------------------
#include <iostream>

template<typename T> struct dllmain_foo
{
        dllmain_foo();
};

template<typename T>
dllmain_foo::dllmain_foo()
{
        std::cout << "dllmain_foo constructed" << std::endl;
}

typedef dllmain_foo<int> dllmain_foo_int;

static dllmain_foo_int dllmain_foo_int_wombat;
----------------------------------------------------------------------

I'd expect "dllmain_foo constructed" to show up when tested in a
console-mode app, but it doesn't. If I make dllmain_foo just a regular
structure (non-template), the expected output shows up.


-scooter
Jan 27 2004
↑ ↓ Scott Michel <scottm cs.ucla.edu> writes:
Slightly better console mode program that demonstrates that the static 
template object's ctor doesn't get invoked. Oddly enough, I could work 
around this problem with an anonymous namespace:

#include <iostream>

//---------------------------------------------------------------------------

template<typename T>
struct problem_1
{
     problem_1();
    ~problem_1();
};

template<typename T>
problem_1<T>::problem_1()
{
     std::cout << "problem_1 ctor invoked." << std::endl;
}

template<typename T>
problem_1<T>::~problem_1()
{
     std::cout << "problem_1 dtor invoked." << std::endl;
}

// Should be initialized but isnt (no output from ctor):
static problem_1<int> static_problem_1;

//---------------------------------------------------------------------------

namespace {
         template<typename T>
         struct problem_2
         {
             problem_2();
            ~problem_2();
         };

         template<typename T>
         problem_2<T>::problem_2()
         {
             std::cout << "problem_2 ctor invoked." << std::endl;
         }

         template<typename T>
         problem_2<T>::~problem_2()
         {
             std::cout << "problem_2 dtor invoked." << std::endl;
         }
};

// But the anonymous namespace DTRT:
problem_2<int>  anon_problem_2;

//---------------------------------------------------------------------------

int main(void)
{
     std::cout << "hello from main." << std::endl;
}
Feb 02 2004
↑ ↓ Scott Michel <scottm cs.ucla.edu> writes:
Scott Michel wrote:

 Slightly better console mode program that demonstrates that the static
 template object's ctor doesn't get invoked. Oddly enough, I could work
 around this problem with an anonymous namespace:

Let me clarify: I can work around the problem in the example's code with an anonymous namespace. I can't necessarily be so cavalier with my research code. -scooter
Feb 02 2004
↑ ↓ → "Walter" <walter digitalmars.com> writes:
"Scott Michel" <scottm cs.ucla.edu> wrote in message
news:bvmqak$2tvn$1 digitaldaemon.com...
 Scott Michel wrote:

 Slightly better console mode program that demonstrates that the static
 template object's ctor doesn't get invoked. Oddly enough, I could work
 around this problem with an anonymous namespace:

Let me clarify: I can work around the problem in the example's code with

 anonymous namespace. I can't necessarily be so cavalier with my research
 code.

I understand, and I can reproduce the problem. I'll try and fix it.
Feb 06 2004