I have a C++ program that crashed due to a bug. It would not even get to main as a NULL pointer was accessed in one of its static global object's contructor functions. To make matters worse the pointer was NULL but should have been set by another global static variable. I think I can wrap those globals in a function that sets global pointers to the objects. I know that need to redesign the globals out. I also know that the order of global static objects is not known. I remain curious--is there is a good accepted method to control the order of static global object initialization?
SIMPLIFIED EXAMPLE (REMOVED ERROR CHECKING LOGGING ETC)
[game.cpp]
Point UpperLeft(-1024.1024); //Global Evilness
int InitializeGobals()
{
...
}
int main (int argc, char ** argv)
{
if (!InitializeGame())
Die("Initialization Failure");
WriteHighScore(UpperLeft);
}
I want to specify the all the static objects in order using:
[prposedgame.cpp]
Point *UpperLeft;
int InitializeGobals()
{
Point IGUpperLeft = new Point(-1024.1024);
UpperLeft = &IGUpperLeft;
...
}
int main (int argc, char ** argv)
{
if (!InitializeGame())
Die("Initialization Failure");
WriteHighScore(UpperLeft);
}
[NOT REALLY RELEVANT BUT MAY HELP UNDERSTANDING THE PROBLEM]
[Point.cpp]
Point::Point() {
x = 0;
y = 0;
}
Point::Point(int nx, int ny) {
x = nx;
y = ny;
strcpy(text,0); //0 should have been set by another global static object
}
[Point.h]
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point();
Point(int nx, int ny);
int getX();
int getY();
int getText(*pcbuff);
private:
int x;
int y;
char text[2];
};
#endif