Hi all, I just found my sfml demo has a different exit behaviour.
If i close the demo directlly by closing the console, my log shows that after the exit, it launches some destructor from another thread, but i'm not using multi-thread currentlly.
And the exec will crash if i don't comment the lock in my log class's write function.
std::lock_guard<std::mutex> lock(m_StreamMutex);
if (m_ConsoleOutput)
{
m_Formatter(m_ConsoleStream, record);
m_ConsoleStream << record.stream.str() << "\n";
}
if (m_FileOutput)
{
m_Formatter(m_FileStream, record);
m_FileStream << record.stream.str() << "\n";
m_FileStream.flush();
}
Btw, it seems with this approach, my code won't reach those codes afert the main loop.
// Game Loop
sf::Clock clock;
sf::Event event;
sf::Int32 next_game_tick = clock.getElapsedTime().asMilliseconds();
while (window.isOpen() && example->running)
{
// Update game logic TICKS_PER_SECOND times per second.
int numLoops = 0;
while (clock.getElapsedTime().asMilliseconds() > next_game_tick && numLoops < MAX_FRAMESKIP && example->running)
{
example->PreFixedUpdate();
while (window.pollEvent(event))
{
example->HandleEvent(event);
}
example->FixedUpdate();
example->PostFixedUpdate();
next_game_tick += SKIP_TICKS;
numLoops++;
}
// display game object in maximum framerate.
float dt = float(clock.getElapsedTime().asMilliseconds() + SKIP_TICKS - next_game_tick) / float(SKIP_TICKS);
example->Update(dt);
window.setActive();
example->Draw(window);
window.display();
window.setActive(false);
}
If i close the window by the window's exit button, everything works fine.
And i can reach the codes after the main loop.
This is how i log the thread_id, if that matters.
void Simple(std::ostream& stream, const Record &record)
{
stream << "[" << record.level << "]";
if (!ThreadManager::IsMainThread())
stream << "[" << std::this_thread::get_id() << "]";
stream << "[" << record.func << "][" << record.line << "]: ";
};
What's wrong, why they behaves different. Am i missing sth?
I'm on windows with MSVC2010, sfml 2.3.2, c++11.