I used STL list in my win32 program, which run into a weird problem. To be specific, I got an out of range runtime error on the first time the list did a push_back().
Here’s the declaration of this list instance:
AtomActionList g_AtomActions[MAXPLAYER];
It’s a global variable and in the same file with _tWinMain() entry.
Here’s the related defines (they are separated in 3 different header files):
#define MAXPLAYER 2
typedef int AtomAction_id;
typedef std::list<AtomAction_id> AtomActionList;
And here’s the parts that ever use g_AtomActions in the main file:
...
g_AtomActions[_i].push_back(aaid);
...
if (!g_AtomActions[_i].empty()){
shareData.newAtomAction[_i] = g_AtomActions[_i].front();
g_AtomActions[_i].pop_front();
}
There used to be a clear method in an init function but I commented it out when I saw the runtime “out of range” error in the g_AtomActions[_i].push_back(aaid); part. However, the error remains after that.
I’ve set some breakpoints and run it in debug mode and watch. I’m quite sure the error happens in the first time g_AtomActions[_i].push_back(aaid); is executed. _i is 0 and aaid is 2, size of g_AtomActions[_i] is 0. No other parts have ever used this identifier yet.
I failed to figure out why. So I tried to change it to deque, hoped to get some luck, and unexpectedly found the g_AtomActions[_i].push_back(aaid); works in my program (but errored again in another part which used list).
Could someone give a possible explanation?
Thanks to hints from @AlfP, this problem is finally resolved by me.
Global variables are very bad. Some classes failed to initialize but never telled me anything until I use pointers to
newthem in another init function.