After an hour of painfull debugging I have figured out that program will crash ON EXIT if during run-time it enters multiple "main" game loops.
Weird? I know right?
This is my example:
GameLoop calls the Encounter:
void Game::GameLoop()
{
Map Map(Window, World, *this);
Player Player(Map, LoadFileName);
while(Window.IsOpened())
{
sf::Event Event;
while(Window.PollEvent(Event))
{
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
return;
else if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Up))
{
switch(Player.Move(NORTH))
{
//...
case 2:
Encounter(Map, Player, NORTH);
break;
}
}
//...
}
Window.Clear();
DrawAll(Map, Player);
Window.Display();
}
}
Then Encounter creates Combat object and enters new loop:
void Game::Encounter(Map &map, Player &player, Orientation Direction)
{
switch(Direction)
{
case NORTH:
switch(player.InteractsWith)
{
case ENEMY:
for(auto itr = Enemies.begin(); itr != Enemies.end(); itr++)
{
if(itr->GetX() == player.GetX()/32 && itr->GetY() == player.GetY()/32-1)
{
Combat Combat(Window, player, *itr);
switch(Combat.MainLoop())
{
//...
}
return;
}
}
break;
//...
}
break;
//...
}
}
Then in Combat::MainLoop() stuff happens:
int Combat::MainLoop()
{
//...
while(window.IsOpened())
{
while(window.PollEvent(Event))
{
//...
}
window.Clear();
//...
window.Display();
}
}
Now. For sake of debugging, I put the return; right before while(window.PollEvent(Event)), and program did not crash upon game exit. Also, if i put return on next line, it will crash on exit.
These are crash "details":
Problem signature:
Problem Event Name: APPCRASH
Application Name: 2D Krofna Game Project.exe
Application Version: 0.0.0.0
Application Timestamp: 4f240720
Fault Module Name: atioglxx.dll
Fault Module Version: 6.14.10.11318
Fault Module Timestamp: 4ebb3dc2
Exception Code: c0000005
Exception Offset: 0086c161
OS Version: 6.1.7600.2.0.0.256.1
Locale ID: 1050
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
Any idea why this happens?