I've been struggling with a rather baffling problem: I occasionally get segmentation faults when my entity manager iterates through the map of entities in the update loop. The strange thing is that this doesn't happen all the time; sometimes it will crash on loading and sometimes I can switch between app states (and load and unload entities many times) a few times before I get the segfault. I also seem to get more segfaults in debug mode. My entities consist of pointers to a Behavior and Drawable class.
My call stack after the segfault:
#0 6FCB4986 libstdc++-6!_ZSt18_Rb_tree_incrementPSt18_Rb_tree_node_base() (C:\MinGW\bin\libstdc++-6.dll:??)
#1 0040A1D7 std::_Rb_tree_iterator<std::pair<unsigned int const, Entity*> >::operator++(this=0x28fe94) (c:/mingw/bin/../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:196)
#2 00401F55 EntityManager::onLoop(this=0x417238) (C:\Users\Nelarius\Documents\Kurssit\Miinaharava\src\engine\EntityManager.cpp:75)
#3 00401640 App::onLoop(this=0x417040) (C:\Users\Nelarius\Documents\Kurssit\Miinaharava\src\engine\App.cpp:38)
#4 0040160C App::execute(this=0x417040) (C:\Users\Nelarius\Documents\Kurssit\Miinaharava\src\engine\App.cpp:30)
#5 00403BD7 main(argc=1, argv=0x642908) (C:\Users\Nelarius\Documents\Kurssit\Miinaharava\src\main.cpp:15)
Here's my update loop:
void EntityManager::onLoop()
{
std::map<const unsigned int, Entity*>::iterator it;
for (it = _gameObjects.begin(); it != _gameObjects.end(); it++)
{
Behavior* behavior = it->second->getBehavior();
if (behavior)
{
behavior->update();
}
}
}
I get the segfault at
for (it = _gameObjects.begin(); it != _gameObjects.end(); it++)
By the way, is it normal for there to be two threads when I'm not using any multithreading? I was looking at the Code::Blocks
debug windows, and happened to see that there were two threads in the thread watch window (only one of which was active though).