Skip to main content
gamedev.net
🔒 Locked

static initialization problem

Started by alich 4 replies 1.1k views
Original Post
alich
alich
Hello, I have a list of classes which all in some way needs to be initialized before any code is executed, to accomplish this I have created a simple maintain class:

template <typename Type>
class maintain_class {
public:
	maintain_class() {
		Type::init_class();
	}
	~maintain_class() {
		Type::release_class();
	}

};
Now, in each cpp defining the implementation of a certain class i create a static instance of this maintain_class. This should work and to my understanding of the c++ standard this variable should be instantiated before any other code. This does work in most cases, but in some I don't explicitly use the class in question and in those seldom cases the maintain_object will not be instantiatet. BUT if I create an instance of the class "being maintained" (note: i dont need to create the maintain_class object myself) it all works perfectly. Is this a compiler bug or not? I have checked the compiler settings and I could not find anything solving my problem. The compiler in use is MSVS71. Regards References: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_initialization_of_static_objects.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_additional_startup_considerations.asp
Daher_Q3A
Daher_Q3A
creating a global instance in the defining module does not garantee that this code is executed first.

take this example:

module 1;

maintain_class f;


module 2;

foo b;


nothing ever said in the standards that module 1 is going to be instentiated first.

let the constructor of foo check if it is the first time an object of type foo is being created, and if so, call the initiate_class static member, and raise the flag. this way, if your application doesn't use anything that requires this code to be executed, then it will not be executed...

take a look here:
class foo    {        static int counter;        static void init_class() { }        static void release_class() { }    public:         foo()            {            if( foo::counter++ == 0 )                {                foo::init_class;                }            }        ~foo()            {            if( -- foo::counter == 0 )                {                foo::release_class();                }            }    };int foo::counter = 0;


or simply create a flag, and in the end of your application call foo::release_flag manually...
Leadorn
Leadorn
dosent work if you use factory and client/server.
Leadorn
Leadorn
but dosent the standard say that it should be initialized some time before main. What order it is initialized dosent matter in our case only that it will be initialized before main.
Leadorn
Leadorn
a link that describes the problem when using dll, we where using dlls but are now using static linked librarys.


http://interreality.org/~reed/dylib_init.html
alich
alich
Quote:

creating a global instance in the defining module does not garantee that this code is executed first.


As leadorn say, we have no issue with the order of initialization, we have a issue that the static variable is not initialized at all.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.