C++ global variable lifespan
I have a global variable which is an instance of a class. This class created an image in its constructor (directX).
The problem is that I am getting an access violation at runtime but the code compiles. I think the problem is that the cl开发者_如何学运维ass constructor is being called before the initialisation done in the winmain function.
so what I want to know is
has anyone encountered this problem and knows of a solution.
What is the lifespan of a global, i know variables declared in a function are lost after it returns and that the compiler looks through the code the see if everything matches which is why we have to prototype functions but where do global's come into the equation.
You probably want to look at something like the singleton pattern if you really want to have one instance of a global, that can be initialized after the initialisation is done (essentially, the image would be constructed the first time you referenced it after which you'd use the pre-constructed version).
Globals are constructed (in undefined order), before your winmain is called. They stay there until your program exits (at which point I believe the destructors are called in an undefined order)..
Another (possibly simpler) alternative you could use would be to change your global from an instance of the class to a pointer to it... then you'd have something like:
// global...
MyGlobalClass *bigGlobalImageHolder;
// Winmain
// Perform directX setup (don't know what that is)
// Create the image class
bigGlobalImageHolder = new MyGlobalClass();
// do the rest of your stuff... I'm guessing enter some kind of event loop
// clean up your global
delete bigGlobalImageHolder;
// exit your winmain (and application)
Then everywhere you're currently referencing your global, you could reference it via a pointer instead..
// so
bigGlobalImageHolder.GetImage();
// becomes
bigGlobalImageHolder->GetImage();
It appears you are experiencing the "static initialization order fiasco".
Check this this and the following faqs.
As already mentioned above, you'll probably want to implement one or another kind of Singleton.
- I have faced this problem; Whatever code is used in that
class constructor
should be initialized before theclass constructor
starts. Suppose if the other variables are also globals then their constructor should be called first. Though constructors forglobal
variables are called in undefined way, only small work around you can use is, define the dependent global variables in same files in order from least dependent to most dependent. - Global lifespan is equal to your program lifespan
The lifetime of global statical data is for the lifetime of the application. However, on normal termination, destructors will run.
The problem with this is that the order in which initialization/destruction take place is not always deterministic, and certainly isn't portable (i.e. depends on platform and e.g. runtime linkers).
The situation is worst with shared object (dynamic linking). Some platforms allow to use linker flags to specify 'priorities' for shared objects (so that the static initialization order can be influenced); Other platforms (e.g. AIX gcc, HP/UX) require special compiler attributes to mark static procedures as 'dll initialization routines'.
In short: do not depend on statical data, except through accessor functions. An accessor function might look like this:
static int _myinternalvar = 3;
static int GetMyInternalVar()
{
return _myinternalvar;
}
this way, you will know for sure that the static was initialized before the function is executed. It the accessor is the only function that can access the data, you can declare the static within it's scope (adding encapsulation).
精彩评论