Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: exit behavies different by close console/window  (Read 1783 times)

0 Members and 1 Guest are viewing this topic.

sindney

  • Newbie
  • *
  • Posts: 4
    • View Profile
exit behavies different by close console/window
« on: April 18, 2016, 01:40:54 pm »
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.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: exit behavies different by close console/window
« Reply #1 on: April 18, 2016, 01:45:59 pm »
Closing the console window is not a clean way to shutdown your application, it's more like calling the exit function.

Always close the window to shutdown your application.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sindney

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: exit behavies different by close console/window
« Reply #2 on: April 18, 2016, 01:52:22 pm »
 :D Got it, thanks~!