调用sf :: Window :: close之后,SFML中出现分段错误

时间:2019-07-25 09:57:35

标签: c++ multithreading segmentation-fault sfml

我试图在与main()分开的线程上运行SFML窗口。调用sf :: Window :: close不会引起任何直接的问题,但是在main()的末尾,可能是当UI对象被破坏时,会发生分段错误。如果不调用sf :: Window :: close,则不会发生分段错误。

我正在运行完全更新的Debian 10安装。

#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();
}

1 个答案:

答案 0 :(得分:1)

我发现了问题。您必须先关闭窗口,然后再关闭另一个线程。我最初是在文档中错过了这一点。

#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();
}