Search code examples
c++multithreadingsegmentation-faultsfml

Segmentation Fault in SFML After calling sf::Window::close


I'm trying to run an SFML window on a separate thread from main(). Calling sf::Window::close doesn't cause any immediate problems, however at the end of main(), possibly when the UI object is destructed, a segmentation fault error occurs. No segmentation fault occurs if sf::Window::close isn't called.

I'm running a fully updated Debian 10 install.

#include <thread>
#include <SFML/Graphics.hpp>

int main() {
    sf::Window window(sf::VideoMode(500,500), "Test");

    std::thread th(&sf::Window::close, &window);
    th.join();
}

Solution

  • I found the problem. You have to deactivate the window before closing in another thread, like so. I missed this in the documentation initially.

    #include <thread>
    #include <SFML/Graphics.hpp>
    
    int main() {
        sf::Window window(sf::VideoMode(500,500), "Test");
    
        window.setActive(false);
    
        std::thread th(&sf::Window::close, &window);
        th.join();
    }