Thread overview
static object constructors not being invoked in a DLL
Jan 28, 2004
Scott Michel
Feb 02, 2004
Scott Michel
Feb 03, 2004
Scott Michel
Feb 06, 2004
Walter
January 28, 2004
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
February 02, 2004
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;
}

February 03, 2004
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
February 06, 2004
"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
an
> 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.