c++ - static object constructors not being invoked in a DLL
- Scott Michel (21/21) Jan 27 2004 Here's the simplified code example that demonstrates that static templat...
- Scott Michel (49/49) Feb 02 2004 Slightly better console mode program that demonstrates that the static
- Scott Michel (5/8) Feb 02 2004 Let me clarify: I can work around the problem in the example's code with...
- Walter (4/11) Feb 06 2004 an
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
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 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
"Scott Michel" <scottm cs.ucla.edu> wrote in message news:bvmqak$2tvn$1 digitaldaemon.com...Scott Michel wrote:anSlightly 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 withanonymous 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